function anchor($text)
{
return preg_replace('#'>'>([0-9]+)#','<span class=anchor><a href="#$1">>>$1</a></span>', $text);
}
这段代码用于呈现页面锚点。我需要使用
([0-9]+)
部分作为变量来执行一些数学运算来定义 href 标记的确切 URL。谢谢。
改用preg_replace_callback。
在 php 5.3 + 中:
$matches = array();
$text = preg_replace_callback(
$pattern,
function($match) use (&$matches){
$matches[] = $match[1];
return '<span class=anchor><a href="#$1">'.$match[1].'</span>';
}
);
在 php <5.3 中:
global $matches;
$matches = array();
$text = preg_replace_callback(
$pattern,
create_function('$match','global $matches; $matches[] = $match[1]; return ''<span class=anchor><a href="#$1">''.$match[1].''</span>'';')
);