The functions below, will do a replace on the content (which is html markup) wrapping bold and em tags around the first two occurrences of the keyword that it finds.
The one case I need to account for though, is if the keyword is already inside of an h1 tag I don't want the callback to occur.
Example:
<h1>this is the keyword inside of a heading tag</h1>
After replacement
<h1>this is the <b>keyword</b> inside of a heading tag</h1>
How might I alter the replacement so that it skips over keywords that appear inside a heading tag (h1-h6) and moves on to the next match?
function doReplace($matches)
{
static $count = 0;
switch($count++) {
case 0: return ' <b>'.trim($matches[1]).'</b>';
case 1: return ' <em>'.trim($matches[1]).'</em>';
default: return $matches[1];
}
}
function save_content($content){
$mykeyword = "test";
if ((strpos($content,"<b>".$mykeyword) > -1 ||
strpos($content,"<strong>".$mykeyword) > -1) &&
strpos($content,"<em>".$mykeyword) > -1 )
{
return $content;
}
else
{
$theContent = preg_replace_callback("/\b(?<!>)($mykeyword)\b/i","doReplace", $content);
return $theContent;
}
}