在下面的函数中,save\\u content函数运行,但doReplace不运行(echo
“这是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;
}
SO网友:Denis de Bernardy
preg\\u 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);