1

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);
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
Engin
  • 13
  • 2
  • 1
    Please format your question, it makes no sense as it is – mayersdesign Apr 07 '17 at 14:47
  • 1
    Welcome to Stack Overflow! [Obligatory link](http://stackoverflow.com/a/1732454/2307070) about why ṫ̨̗̺̭̮̞̗̜̮̗̙̫̺̖̭̯͊ͨ̌͒̍͘͘͟͝h̸͓̩̙͙̻̗͔̞̘̟̩̯͋͑͂͐a̴̧ͨ́ͭ͒ͯ̓͐̇̃ͥ͢҉‌​̨̳̜̤͍͖t̵̳̳͕͉͋̓͐ͦͬ̈́̀̚‌ is a bad idea – Thomas Ayoub Apr 07 '17 at 14:59

1 Answers1

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

revo
  • 47,783
  • 14
  • 74
  • 117