Regex / DOMDocument - match and replace text not in a link

后端 未结 7 1127
轮回少年
轮回少年 2020-12-01 07:06

I need to find and replace all text matches in a case insensitive way, unless the text is within an anchor tag - for example:

Match this text and re

7条回答
  •  既然无缘
    2020-12-01 07:25

    $a='

    Match this text and replace it

    Don\'t match this text

    We still need to match this text and replace it

    '; echo preg_replace('~match this text(?![^<]*)~i','replacement',$a);

    The negative lookahead ensures the replacement happens only if the next tag is not a closing link . It works fine with your example, though it won't work if you happen to use other tags inside your links.

提交回复
热议问题