title: PHP RegEx Match Text NOT in Anchor Tag
body: <p>Regex is a bit over my head, but I'm trying to learn.</p>
  
  <p>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.</p>
  
  <p>Here's the (updated) pattern:</p>
  
  <pre><code>/(?!(?:[^&lt;]+&gt;|[^&gt;]+&lt;\/a&gt;))\b(Match Me)\b/is
  </code></pre>
  
  <p>And some sample text:</p>
  
  <pre><code>&lt;a href="#"&gt;Don't Match Me &lt;span&gt;web&lt;/span&gt;&lt;/a&gt;
  &lt;a href="#"&gt;Don't Match Me&lt;/a&gt;
  Match Me
  &lt;span&gt;Match Me&lt;/span&gt;
  </code></pre>
  
  <p>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 &amp; 4th lines.</p>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>preg_replace($searchFor, $linkArray, $content);
  </code></pre>
  
  <p>where $searchFor and $linkArray are both multidimensional arrays with corresponding keys holding the pattern and replacement html respectively.</p>
  
  <p>Any help is appreciated!</p>