【发布时间】:2017-03-14 23:10:21
【问题描述】:
我正在尝试编写一个函数来替换任何以 PID 开头和结尾的数字。我认为问题出在正则表达式中,但我无法弄清楚。代码如下。我也尝试了 (.) 而不是 (.*) - 没有区别。我知道我将不得不循环遍历匹配数组以获取所有匹配项,但我想首先让基本代码工作。有人能指出问题吗?
function DoLinks($matches) {
return ($matches[0]='<a href="someplace.com">Some place</a>');
}
function Buildinks($text) {
preg_replace_callback(
"/PID(.*)PID/",
"DoLinks",
$text);
return $text;
}
$text = "hello PID(199)PID";
$text = Buildinks($text);
echo $text;
【问题讨论】:
-
你想用
"hello <a href="someplace.com">Some place</a>"替换"hello PID(199)PID"吗? -
是的,没错。 LukStorms 发布的代码正是我想要做的。
标签: php regex preg-replace-callback