且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

正则表达式:如何不替换任何html标签中的特定单词?

更新时间:2022-06-02 23:18:07

与html字符串进行交互的快速,较不可靠的方法是使用正则表达式.DomDocument(或类似文件)是专门设计用来解析html的文件,它的可信度要高得多.我将发布正则表达式方式,如果可以管理它,则将添加DomDocument方式.

The quick, less reliable way to interact with html strings is with regex. DomDocument (or similar) is specially designed to parse html and is far more trustworthy. I'll post the regex way, and if I can manage it, I'll add a DomDocument way.

(* SKIP)(* FAIL)允许您匹配/使用和取消对子字符串的资格,然后在管道之后为实际要替换的子字符串编写模式.

(*SKIP)(*FAIL) allows you to match/consume and disqualify substrings then after the pipe you write the pattern for the substring that you actually want to replace.

模式:〜(?:< [^>] *&.; *?</[^>] *>(* SKIP)(* FAIL))| \ btest \ b〜s

替换:< span style ="color:#FF0000;"> \ 0</span>

模式演示

代码:( 演示)

$string="This is a great test! We're testing something awesome. Click here to <a href=\"whatever\">test it!</a>.";
$pattern='~(?:<[^>]*>.*?</[^>]*>(*SKIP)(*FAIL))|\btest\b~s';
$replace='<span style="color: #FF0000;">\0</span>';
echo preg_replace($pattern,$replace,$string);

输出:

This is a great <span style="color: #FF0000;">test</span>! We're testing something awesome. Click here to <a href="whatever">test it!</a>.