dongtang4954
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:

/\b(SEARCH_TERM)\b/

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="http://www.google.com?q=my+test+string">link</a>

Would return:

my SEARCH_TERM string <a href="http://www.google.com?q=my+SEARCH_TERM+string">link</a>

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

my SEARCH_TERM string <a href="http://www.google.com?q=my+test+string">link</a>

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

/(?<!(href|src)=.*)\b(SEARCH_TERM)\b/

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 属性有时可能在其路径中包含要替换的单词。 如果在这些元素中替换此单词, 然后链接或图像将不再起作用。

示例,将“test”替换为“SEARCH_TERM”以获取以下示例字符串:

   my test string&lt; a href =“http://www.google.com?q=my+test+string”&gt; link&lt; / a&gt; 
   
 \  n 

将返回:

 我的SEARCH_TERM字符串&lt; a href =“http://www.google.com?q=my+SEARCH_TERM+string”&gt; 链路&LT; / A&GT; 
   
 
 

我需要它来忽略href属性文本并返回:

 我的SEARCH_TERM字符串&lt; a href =“http:  //www.google.com?q=my+test+string">link</a>
nn

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

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

注意:我特别需要使用Regex,而不是DOM解析器。

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

1条回答 默认 最新

  • dongmuyuan3046 2013-07-03 05:28
    已采纳

    As I mentioned you need to use an html parser.

    But if you want it

    /\btest\b(?=[^>]*(<|$))/s
    

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


    NOTE

    This would not work if your text itself contains >.

    For example

     hello>world
    

    Hence the reason you should use a parser

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题