regex - PHP Match phrase only if outside of an <a href> tag -


i'm trying automate internal links within pages on website. need linked text exempted.

for following example should replace bolded text (example).

<a class="example" href="http://www.example.com/">example</a>  <p>new **example**</p> 

i tried following didn't work.

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

you should follow @thomasayoub's advice. use dom functionalities achieve such task way match desired word regular expressions if want see:

<a\b[^>]+>.*?<\/a>\k|example 

live demo


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -