I have keyword “bolding” function here:
$ignore = array ('div', 'class', 'high', 'light', 'highlight');
$keywords = explode(' ', $qsvarus);
$title[$key] = preg_replace('/'.implode('|', $keywords).'/i', '<b>$0</b>', $title[$key]);
$infoo[$key] = preg_replace('/'.implode('|', $keywords).'/i', '<b>$0</b>', $infoo[$key]);
The problem is it sometimes catches some of my html tags. How to tell it to ignore everything shorter than 3 letters and certain specific words from $ignore array when <b></b> $keywords?
I would simply loop around the keywords array first, removing any matches in the ignore array (use in_array) and also any keywords that are 3 characters or less.
Then if you want to make sure you are not in a tag, the following should suffice:
I’ve basically appended a negative lookahead:
The regex will match as long as that look ahead for a closing html
>not preceded by an opening tag<doesn’t match. I you do get a closing tag, you should be able* to assume that you are inside a tag.Putting that back into the preg_replace:
>)