2013-07-03 05:18
浏览 29

PHP:正则表达式使用Lookbehind Assertions中的通配符替换单词

I'm looking to create a PHP Regex script that can match and replace words within a string.

The regex needs to match only complete words, which I can easily accomplish with:


The problem I am having is that some of the strings contain html elements as such as <a> tags and <img> tags, where the href and src attributes may sometimes contain the to-be-replaced word within their path. If this word is replaced within these elements, then the link or image will no longer work.

Example, replace the word 'test' with 'SEARCH_TERM' for the following example string:

my test string <a href="">link</a>

Would return:

my SEARCH_TERM string <a href="">link</a>

Whereas I need it to ignore the href attribute text and return:

my SEARCH_TERM string <a href="">link</a>

I've looked at using Regex's Lookbehind Assertions (As just below), but variable length wildcard characters are not allowed.


Note: I specifically need to do this with Regex, and not a DOM parser.

图片转代码服务由CSDN问答提供 功能建议

我正在寻找一个可以匹配和替换字符串中的单词的PHP Regex脚本。


  / \ b(SEARCH_TERM)\ b / 

我遇到的问题是某些字符串包含html元素,例如&lt; a&gt; 标记和&lt; img&gt; 标记,其中 href src 属性有时可能在其路径中包含要替换的单词。 如果在这些元素中替换此单词, 然后链接或图像将不再起作用。


   my test string&lt; a href =“”&gt; link&lt; / a&gt; 
 \  n 


 我的SEARCH_TERM字符串&lt; a href =“”&gt; 链路&LT; / A&GT; 


 我的SEARCH_TERM字符串&lt; a href =“http:  //">link</a>

我看过使用正则表达式的Lookbehind断言( 如下所示),但不允许使用可变长度的通配符。

  /(?&lt;!(href | src)=。*)\ b(SEARCH_TERM)\ b  / 


  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongmuyuan3046 2013-07-03 05:28

    As I mentioned you need to use an html parser.

    But if you want it


    Above regex would match only if there's < or end of string(not line) ahead somewhere without matching >


    This would not work if your text itself contains >.

    For example


    Hence the reason you should use a parser

    解决 无用
    打赏 举报

相关推荐 更多相似问题