preg_replace 在片段中不起作用

preg_replace not working in snippet

我打算使用以下 modx evo 片段(将其命名为 "removespace")来输出电视,删除字符串中的任何 space:

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

但是在模板中调用片段 [[removespace]] 不会生成删除了 space 的字符串。它按原样生成字符串。

但是在 $string1 变量中插入文本 "Hello world" 会产生没有任何 space.

的结果

有什么解决办法吗?

您不能像片段中那样使用 MODX 标签,您需要使用 $modx->documentObject['variable-name']

所以你的代码将是这样的:

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