我有一些像这样的代码(这是一个简化的例子):
function callback_func($matches) {
return $matches[0] . "some other stuff";
}
function other_func($text) {
$out = "<li>";
preg_replace_callback("/_[a-zA-Z]*/","callback_func",$desc);
$out .= $desc ."</li> \r\n";
return $out;
}
echo other_func("This is a _test");
Run Code Online (Sandbox Code Playgroud)
这个的输出应该是
<li>This is a _testsome other stuff</li>
Run Code Online (Sandbox Code Playgroud)
但我得到了
<li>This is a _test</li>
Run Code Online (Sandbox Code Playgroud)
我做错了什么/安抚php神需要什么奇怪的咒语?
preg_replace_callback不会修改字符串,而是返回它的修改后的副本.尝试以下instread:
function other_func($text) {
$out = "<li>";
$out .= preg_replace_callback("/_[a-zA-Z]*/","callback_func",$desc);
$out .= "</li> \r\n";
return $out;
}
Run Code Online (Sandbox Code Playgroud)