2017-04-07 57 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