简体   繁体   中英

RegEx match phrase from html except links and headings

I need to replace phrase to link from html(description), but not from links and headings. For one description can replace several phrases. I have about 100 000 descriptions to replace.

I used this code:

preg_replace("/(\bcustom phrase\b)(?![^<a]*>|[^<>]*.*<\/a>)(?![^<h[0-9]]*>|[^<>]*.*<\/h[0-9]>)/iu", "<a href=\"https://myurl.com\">$1</a>", $text);

It does not work, when phrase is before any link. I think I got lost and need help.

Example for "ipsum dolor"

Lorem ipsum dolor sit amet -> Lorem <a href="https://myurl.com">ipsum dolor</a> sit amet.
Lorem <a href="https://test.com">ipsum dolor</a> sit amet. -> Lorem <a href="https://test.com">ipsum dolor</a> sit amet.
<h1>Lorem ipsum dolor sit amet.</h1> -> <h1>Lorem ipsum dolor sit amet.</h1>
Lorem ipsum dolor sit <a href="https://test.com">amet.</a> -> Lorem ipsum dolor sit <a href="https://test.com">amet.</a> - It does not work

我对问题的回答:

preg_replace("/<a.*?<\/a>(*SKIP)(*F)|<h[0-9]{1}.*?<\/h[0-9]{1}>(*SKIP)(*F)|<.*?>(*SKIP)(*F)|(\bcustom phrase\b)/iu", "<a href=\"https://myurl.com\">$1</a>", $text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM