2017-04-07 69 views
1

I'm trying automate internal links within pages on my website. I need already linked text to be exempted.PHP匹配短語只有在以外<a href> tag

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

請格式化您的問題,這是沒有意義的,因爲它是 – mayersdesign

+1

歡迎來到堆棧溢出! [義務鏈接](http://stackoverflow.com/a/1732454/2307070)關於爲什麼ṫ̨̗̺̭̮̞̗̜̮̗̙̫̺̖̭̯͊ͨ̌͒̍͘͘͟͝h̸͓̩̙͙̻̗͔̞̘̟̩̯͋͑͐a̴̧ͨͭ͒ͯ̓͐̇ͥ͢҉̨̳̜̤͍͖t̵̳̳͕͉͋̓͐ͦͬ̈̚是一個壞主意 –

回答

1

You should follow @ThomasAyoub's advice. Use DOM functionalities to achieve such a task but this is a way to match desired word with Regular Expressions if you want to see:

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

Live demo