2

Regex is a bit over my head, but I'm trying to learn.

I have a working regex I'm using in preg_replace to match a string and replace it with an html link. It correctly avoids when matches are inside anchor tags, unless there is a child tag also in the anchor tag.

Here's the (updated) pattern:

/(?!(?:[^<]+>|[^>]+<\/a>))\b(Match Me)\b/is

And some sample text:

<a href="#">Don't Match Me <span>web</span></a>
<a href="#">Don't Match Me</a>
Match Me
<span>Match Me</span>

The above regex will match the text on the 1st, 3rd and 4th lines. However, I only want to match the text "Match Me" on the 3rd & 4th lines.

NOTE: The content I'm sifting through isn't neatly separated by lines as in my example. It's a paragraph of text in fact.

I'm open to using DOM, but I'm taking a big block of content and applying multiple replacements using preg_replace's array feature, like so:

preg_replace($searchFor, $linkArray, $content);

where $searchFor and $linkArray are both multidimensional arrays with corresponding keys holding the pattern and replacement html respectively.

Any help is appreciated!

danbrellis
  • 370
  • 3
  • 13

0 Answers0