数组中的PHP函数
我的问题是retrieveName没有得到$1的值,但是$1在上一个实例中运行良好数组中的PHP函数,php,Php,我的问题是retrieveName没有得到$1的值,但是$1在上一个实例中运行良好 function bbcode ($string) { // All the default bbcode arrays. $bbcode = array( '#\[quote=(.*?)\](.*?)\[/quote\]#si' => '<span class="bbcode_quote"><b> <a href="userprofi
function bbcode ($string)
{
// All the default bbcode arrays.
$bbcode = array(
'#\[quote=(.*?)\](.*?)\[/quote\]#si' =>
'<span class="bbcode_quote"><b>
<a href="userprofile.php?id='.stripslashes('$1').'" target="_blank">
<span class="fake_link">'.retrieveName('$1').'</span></a> Said:</b><BR/>$2</span><BR/>'
);
$output = preg_replace(array_keys($bbcode), array_values($bbcode), $string);
$output = str_replace("\\r\\n", "<br>", $output);
return $output;
}
假设您使用的是preg_*函数,您应该使用$1而不是\\1。两者都有效,但$1是首选语法
此外,您可能对以下内容之一更感兴趣:
$output = preg_replace("#\[quote=(.*?)\](.*?)\[/quote\]#sie",
"'<span class=\"bbcode_quote\"><b><a href=\"userprofile.php?id='.stripslashes('$1').'\"
target=\"_blank\"><span class=\"fake_link\">'.
retrieveName(stripslashes('$1')) . '</span></a> Said:</b><BR/>$2
</span><BR/>'",$input);
或:
试着这样做:
$output = preg_replace_callback("#\[quote=(.*?)\](.*?)\[/quote\]#si",function($matches) {
return '<span class="bbcode_quote"><b><a href="userprofile.php?id='.stripslashes($matches[1]).'" target="_blank"><span class="fake_link">' .
retrieveName($matches[1]).'</span></a> Said:</b><BR/>'.$matches[2].'</span><BR/>';
},$input);
根据retrieveName中的处理级别,可能需要将其转义为\\\\1。传递给retrieveName的所有变量都以\\开头吗?如果是,为什么不直接传递整数值1,然后在retrieveName函数中附加“\\”?查看retrieveName内部使用可能使用的是stripslash代码应该向函数发送\\1的值,而不是\\1本身。我的问题是,我不确定如何正确引用它,因为它将\\1作为字符串而不是从数组的第一部分发送的变量。是否使用preg_replace_回调?显示所有相关代码。嘿,谢谢。我在第一篇文章中做了一些相应的修改。但是$1仍然没有将值传递给retrieveName;
$output = preg_replace_callback("#\[quote=(.*?)\](.*?)\[/quote\]#si",function($m) {
return '<span class="bbcode_quote"><b><a href="userprofile.php?id=\\1"
target="_blank"><span class="fake_link">' .
retrieveName('\\1') . '</span></a> Said:</b><BR/>\\2
</span><BR/>';
},$input);
$output = preg_replace_callback("#\[quote=(.*?)\](.*?)\[/quote\]#si",function($matches) {
return '<span class="bbcode_quote"><b><a href="userprofile.php?id='.stripslashes($matches[1]).'" target="_blank"><span class="fake_link">' .
retrieveName($matches[1]).'</span></a> Said:</b><BR/>'.$matches[2].'</span><BR/>';
},$input);