title: PHP Simple HTML Dom Parser, Skip word replacement from certain elements
body: <p>I'm trying to add link to keyword with php dom method but the problem is that it's adding links on all instances it can find. I want to skip when keyword already have a link or it's inside img alt="" or title="" attribute, similarly inside any other tag or css class.</p>
  
  <p>The issue is quite similar to this one.  <a href="https://stackoverflow.com/questions/46446272/php-simple-html-dom-parser-find-word">PHP simple html dom parser - find word</a></p>
  
  <p>I tried the answer mentioned in last post with $tagsToExclude array but that's not working.</p>
  
  <p>Here is the code.</p>
  
  <pre><code>&lt;?php
  require_once 'simple_html_dom.php';
  
  $html = new simple_html_dom();
  $html = str_get_html('&lt;html&gt;&lt;body&gt;&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&lt;/p&gt;
  
  &lt;p&gt;Don\'t add link on Lorems&lt;/p&gt;
  
  &lt;p&gt;Add on Lorem again.&lt;/p&gt;
  
  &lt;p&gt;An existing link on &lt;a href="http://lorem.net"&gt;Lorem&lt;/a&gt;&lt;/p&gt;
  
  
  &lt;img src="Lorem.jpg" alt="Lorem text" title="Lorem image"&gt;&lt;/body&gt;&lt;/html&gt;');
  
  
  $nodes = $html-&gt;find('*');
  
  $tagsToExclude = [
      "a",
      "img"
  ];
  
  foreach($nodes as $node) {
  
      $keyword = 'Lorem';
      $link = '&lt;a href="http://example.com"&gt;'.$keyword.'&lt;/a&gt;';
  
      if (!in_array($node-&gt;tag, $tagsToExclude)) {
  
          if(strpos($node-&gt;innertext, $keyword) !== false) {
              $node-&gt;innertext = preg_replace('/\b'.$keyword.'\b/', $link, $node-&gt;innertext);
          }
      }
  }
  
  echo $html-&gt;outertext;
  ?&gt;
  </code></pre>
  
  <p>Thanks</p>