preg_replace 未在 WordPress 插件中替换
跟进我之前的问题,我想用以下格式的链接替换全大写*单词的每个实例:
dictionary.com/browse/<TERM>
我正在使用的 preg_replace
调用是这样的:
$content = preg_replace('#[A-Z][A-Z]+#', '<a href="//dictionary.com/browse/$1">$1</a>', $content);
使用 http://gskinner.com/RegExr,看起来我的正则表达式是正确的,并且它应该< /em> 在每次查找时进行替换。
我是否在 preg_replace
调用中或在将插件/过滤器注册到 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');
* 我使用 [AZ][AZ]+
语法来确保我不匹配“I”和“A”等单词
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>
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);
Using http://gskinner.com/RegExr, it appears I have my regex correct, and that it should be replacing on each find.
Have I done something wrong, either in the preg_replace
call, or pehaps in the registration of the plugin/filter to the Wordpress API?
Full context of the call:
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'm using the [A-Z][A-Z]+
syntax to ensure I do not match words like "I" and "A"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信该函数需要返回替换的结果:
此外,该正则表达式看起来不正确。如果您想匹配全部大写的整个单词,那么
您还需要
$0
(整个匹配),而不是$1
(第一个捕获组,您的正则表达式不支持没有)I believe the function needs to return the result of the replacement:
Also, that regex doesn't look right. If you want to match a whole word in all caps, it's
Also, you want
$0
(the whole match), not$1
(the first capture group, which your regex doesn't have)