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

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

javascript - Confirm a form & display message if form is valid with JQuery -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -