I just looked for a solution to one problem and created one that seems to do the job.
A negative look is the key. To make sure the match is not in the tag, look ahead to make sure that the bracket of the closing corner is not found before opening. Suppose we want to find the word "needle":
#needle(?![^<]+>)#i
My business is in PHP, and it looks something like this:
function filter_highlighter($content) { $patterns = array( '#needle(?![^<]+>)#i', '#<b>Need</b>le#', '#<strong>Need</strong>le#' ); $replacement = '<span class="highlighted">Need</span>le'; $content = preg_replace( $patterns, $replacement, $content); return $content; }
While this is working.
source share