preg_replace无法在代码段中使用

时间:2015-06-19 21:12:36

标签: php modx modx-evolution

我想使用以下modx evo片段(命名为“removespace”)来输出删除字符串中任何空格的电视:

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

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

但是在$ string1变量中插入文本“Hello world”会产生没有任何空格的结果。

任何解决方案?

1 个答案:

答案 0 :(得分:1)

您无法在代码段内使用MODX代码,您需要使用$modx->documentObject['variable-name']

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

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