为什么返回原始字符串而不是编辑过的字符串?
原始字符串:
I cant believe Ed ate [-food-] for breakfast.
替换:
preg_replace_callback('/\[\-[a-zA-Z0-9_]\-\]/', 'tag_func', $email_body);
function tag_func($matches){
$matches[0] = 'tacos';
return $matches[0];
}
结果所需字符串:
I cant believe Ed ate tacos for breakfast.
这是我的警告:
preg_replace_callback(): Requires argument 2, 'tag_func', to be a valid callback
答案 0 :(得分:1)
您忘了添加+
,因此它应匹配多个字符:
preg_replace_callback('/\[-[a-z\d_]+-\]/i', 'tag_func', $email_body);
// ^
在此处查看:[{3}}
如果你正在运行5.3,你可以直接传递一个匿名函数:
preg_replace_callback('/\[-[a-z\d_]+-\]/i', function () {
return 'tacos';
}, $email_body);