Reputation: 762
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?
Upvotes: 0
Views: 633
Reputation: 1239
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:
/\b(keyword|keyword|keyword)\b(?![^<]*[>])/
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:
preg_replace('/\b('.implode('|', $keywords).')\b(?![^<]*[>])/i', '<b>$1</b>', $subject);
>
)Upvotes: 2