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.

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP