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);
答案 0 :(得分:1)
你应该遵循@ ThomasAyoub的建议。使用DOM功能来完成这样的任务,但如果你想看到这是一种将所需单词与正则表达式匹配的方法:
<a\b[^>]+>.*?<\/a>\K|Example