How to replace keywords outside of img tags

I have this existing code:

  foreach ($keyword_array as $kw=>$kwlink){
  	$news_content = preg_replace('@(?<=\W|^)('.$kw.')(?=\W|$)@i', '<a href="'.$kwlink.'">$1</a>', $news_content, 1);

But it also adds links to keywords inside img tags. How can I alter the preg_replace to exclude occurences inside img tags?

Anyone able to help with this?

Where is $keyword_array coming from? i.e. why is it getting both links and images?

Sorry for delay, didn’t get a notification!

The keywords array is a simple array I’ve created to determine what keywords will be replaced with what links.

The code is being run over the variable that holds the main content for the page. It replaces the keywords (once only) with the links if it finds them. This was fine until I started adding tags to my content. It’s now adding the link into the alt text of the image if it finds the keyword there first!

Does that make sense?

Yes, sorry, I meant to get back sooner after giving it some thought but got interupted by RL.

I was thinking how I might go about it and came up with two options but didn’t get much further than

  • Try to craft a better regex (would likely be a gnarly ugly mess and not an easy task).
  • Work with the DOM elements instead of parsing the mark-up as a String.

No worries. Yes, I was hoping someone might be able to help redo the regex?

I did have a look at working with the DOM. Could you give any examples of how I might achieve this using this method instead?

Nevermind, solved by pregmatching the image html, removing it before processing the link replacements and then added it back after! Had a flash of inspiration!