为什么preg_replace_callback永远不会在此功能中发射?
-
16-10-2019 - |
题
在下面的函数中,save_content函数运行,但doreplace却没有(回声
“这是Doreplace”从未显示。有什么想法吗?
add_action('content_save_pre', 'save_content');
function save_content($content){
global $post;
$mykeyword = rseo_getKeyword($post);
$mykeyword = preg_quote($mykeyword, '/');
$content = preg_replace_callback("/\b($mykeyword)\b/i","doReplace", $content);
return $content;
}
function doReplace($matches)
{
echo "This is the doReplace";
die;
}
解决方案
您的功能需要返回数据,而不是回声。
function doReplace($matches) {
return "This is the doReplace";
}
希望有帮助。
其他提示
不应该在数组地图调用中使用Preg_quote()调用吗?如果您将其喂食之类的东西,例如“ foo | bar”,它将被逃脱为“ foo | bar”,它不匹配任何东西...更好地使用类似的东西:
// uncomment in case you're returning a string:
// $kw = explode('|', $kw);
// create a new function if php 5.3 is not an option
$regex = array_map(function($in) {
return preg_quote($in, '/');
}, $kw);
$regex = implode('|', $regex);