Regex / DOMDocument - match and replace text not in a link

Viewed 7053

I need to find and replace all text matches in a case insensitive way, unless the text is within an anchor tag - for example:

<p>Match this text and replace it</p>
<p>Don't <a href="/">match this text</a></p>
<p>We still need to match this text and replace it</p>

Searching for 'match this text' would only replace the first instance and last instance.

[Edit] As per Gordon's comment, it may be preferred to use DOMDocument in this instance. I'm not at all familiar with the DOMDocument extension, and would really appreciate some basic examples for this functionality.

7 Answers
Related