phpmodxmodx-evolution

preg_replace not working in snippet


I am tying to use the following modx evo snippet (naming it "removespace") to output the tv removing any space in the string:

<?php
$string1 = "[*longtitle*]"; 
$string = preg_replace('/\s+/', '', $string1);
return $string;
?>

But calling the snippet [[removespace]] in the template does not produce the string with space removed. It produces the string as is.

But inserting the text "Hello world" in the $string1 variable produces the result without any space.

Any solution?


Solution

  • You can not use MODX tags as is inside the snippets, you will need to use $modx->documentObject['variable-name']

    So your code will be something like this:

    <?php
    $string1 = $modx->documentObject['longtitle']; 
    $string = preg_replace('/\s+/', '', $string1);
    return $string;
    ?>