简体   繁体   English

RegEx匹配html中的短语,链接和标题除外

[英]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. 我需要替换短语以从html(description)进行链接,但无需从链接和标题进行链接。 For one description can replace several phrases. 一个描述可以替换几个短语。 I have about 100 000 descriptions to replace. 我大约有10万个描述要替换。

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" “ 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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