0

我想在 preg_replace中重新格式化preg_replace()匹配。strtr()是否可以 ?

我做了以下事情:

$array = array("#" => "_", "/" => "-");
$output = preg_replace($regex, '<span>'.strtr('$0', $array).'</span>', $input);

在我的示例中,Z#(对应于我的preg_replace匹配项,$0 中的strtr)应该变为 Z_,但没有任何反应。

谢谢 !

注意。$regex 是一个匹配 $input 某些部分的正则表达式,它可以工作。

4

1 回答 1

1

使用e -修饰符:

$output = preg_replace('/$regex/e', '"<span>".strtr("$0", $array)."</span>"', $input);
于 2012-09-13T11:17:07.567 回答