fork(13) download
  1. <?php
  2. $target = "word1 <a skip this word2 >word2 again</a> word3";
  3. $regex = "~<[^>]*>(*SKIP)(*F)|word1|word2|word3~";
  4. $repl= '<span class="">\0</span>';
  5. $new=preg_replace($regex,$repl,$target);
  6. echo htmlentities($new);
  7.  
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
&lt;span class=&quot;&quot;&gt;word1&lt;/span&gt; &lt;a skip this word2 &gt;&lt;span class=&quot;&quot;&gt;word2&lt;/span&gt; again&lt;/a&gt; &lt;span class=&quot;&quot;&gt;word3&lt;/span&gt;