问题描述
跟进我之前的问题,我想用以下格式的链接替换 ALL-CAPS 单词的每个实例:
In follow-up to my previous question, I want to replace every instance of an ALL-CAPS word with a link of the following format:
dictionary.com/browse/<TERM>
我使用的 preg_replace
调用是这样的:
The preg_replace
call I am using is this:
$content = preg_replace('#[A-Z][A-Z]+#', '<a href="//dictionary.com/browse/$1">$1</a>', $content);
使用 http://gskinner.com/RegExr,看来我的正则表达式是正确的,而且它应该在每次查找时进行替换.
Using http://gskinner.com/RegExr, it appears I have my regex correct, and that it should be replacing on each find.
我是不是在 preg_replace
调用中做错了什么,或者是在将插件/过滤器注册到 Wordpress API 时做错了什么?
Have I done something wrong, either in the preg_replace
call, or pehaps in the registration of the plugin/filter to the Wordpress API?
调用的完整上下文:
function define_filter($content){
$content = preg_replace('#[A-Z][A-Z]+#', '<a href="//dictionary.com/browse/$1">$1</a>', $content);
}
add_filter('the_content', 'define_filter');
推荐答案
我认为函数需要返回替换的结果:
I believe the function needs to return the result of the replacement:
return $content;
此外,该正则表达式看起来不正确.如果你想匹配所有大写的整个单词,它是
Also, that regex doesn't look right. If you want to match a whole word in all caps, it's
'#\b[A-Z]+\b#'
此外,您需要 $0
(整个匹配项),而不是 $1
(第一个捕获组,您的正则表达式没有)
Also, you want $0
(the whole match), not $1
(the first capture group, which your regex doesn't have)
这篇关于preg_replace 不在 Wordpress 插件中替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!