我想在preg_replace中重新格式化preg_replace()
与strtr()
的匹配。
有可能吗?
我做了以下事情:
$array = array("#" => "_", "/" => "-");
$output = preg_replace($regex, '<span>'.strtr('$0', $array).'</span>', $input);
在我的示例中,Z#(对应于我的preg_replace
匹配,strtr
中的$ 0)应该变为Z_,但没有任何反应。
谢谢!
NB。 $ regex是一个正则表达式,匹配$ input的某些部分,它可以工作。
答案 0 :(得分:1)
使用 e - 修饰符:
$output = preg_replace('/$regex/e', '"<span>".strtr("$0", $array)."</span>"', $input);