PHP只有在<a href=""> tag

时间:2017-04-07 14:45:03

标签: php regex html5

I'm trying automate internal links within pages on my website. I need already linked text to be exempted.

For the following example it should only replace the bolded text (Example).

<a class="Example" href="http://www.example.com/">Example</a>

<p>New **Example**</p>

I tried the following but it didn't work.

$re = '/(\b'.$search.'\b)+((?=.*<a)|(?!.*<\/a>))/';
$str=preg_replace($re, $replace, $text);

1 个答案:

答案 0 :(得分:1)

你应该遵循@ ThomasAyoub的建议。使用DOM功能来完成这样的任务,但如果你想看到这是一种将所需单词与正则表达式匹配的方法:

<a\b[^>]+>.*?<\/a>\K|Example

Live demo