80giga, this works for me, try to replace:
foreach ($tokens as $token) {
if (++ $j % 2 && $token != '') //-- this token is (non-markup) text
{
if ($anchor_level == 0 && $tag_level == 0) // linkify if not inside anchor tags
{
//-- test for encoded keyword
if (preg_match($match, $token, $kmatch)) {
//-- ensure that the substitute replaces value with the case-sensitive match
if ($match_replace == 1) {
$substitute = preg_replace('/\b' . htmlentities($keyword) . '\b/', $kmatch[0], $substitute);
}
//-- replace keyword with the substitute
$token = preg_replace($match, $substitute, $token);
$must_tokenize = TRUE; //-- re-tokenize next time around
//-- test for non-encoded keyword
}
elseif (preg_match($match_noencode, $token, $kmatch)) {
//-- ensure that the substitute replaces value with the case-sensitive match
if ($match_replace == 1) {
$substitute = preg_replace('/\b' . $keyword . '\b/', $kmatch[0], $substitute);
}
//-- replace keyword with the substitute
$token = preg_replace($match_noencode, $substitute, $token);
$must_tokenize = TRUE; //-- re-tokenize next time around
}
}
} else //-- this token is markup
{
if (preg_match("#<\s*a\s+[^>]*>#i", $token)) { //-- found <a ...>
$anchor_level++;
}
elseif (preg_match("#<\s*/\s*a\s*>#i", $token)) { //-- found </a>
$anchor_level--;
}
elseif (preg_match("#<($blocked_tags)>#i", $token)) { //-- found excluded tags
$tag_level++;
}
elseif (preg_match("#<\/($blocked_tags)>#i", $token)) { //-- found excluded closing tags
$tag_level--;
}
}
$filtered .= $token; //-- this token has now been filtered
}
with
$isfound[$match] = 0;
foreach ($tokens as $token) {
if (++ $j % 2 && $token != '') //-- this token is (non-markup) text
{
if ($anchor_level == 0 && $tag_level == 0 && $isfound[$match] == 0) // linkify if not inside anchor tags
{
//-- test for encoded keyword
if (preg_match($match, $token, $kmatch)) {
//-- ensure that the substitute replaces value with the case-sensitive match
if ($match_replace == 1) {
$substitute = preg_replace('/(\PL|\A)(' . htmlentities($keyword) . ')(\PL|\Z)/i', $kmatch[0], $substitute,1);
}
//-- replace keyword with the substitute
$token = preg_replace($match, $substitute, $token, 1);
$must_tokenize = TRUE; //-- re-tokenize next time around
$isfound[$match]++;
//-- test for non-encoded keyword
}
elseif (preg_match($match_noencode, $token, $kmatch)) {
//-- ensure that the substitute replaces value with the case-sensitive match
if ($match_replace == 1) {
preg_replace('/(\PL|\A)(' . $keyword . ')(\PL|\Z)/u', $kmatch[0], $substitute, 1);
}
//-- replace keyword with the substitute
$token = preg_replace($match_noencode, $substitute, $token,1);
$must_tokenize = TRUE; //-- re-tokenize next time around
$isfound[$match]++;
}
}
} else //-- this token is markup
{
if (preg_match("#<\s*a\s+[^>]*>#i", $token)) { //-- found <a ...>
$anchor_level++;
}
elseif (preg_match("#<\s*/\s*a\s*>#i", $token)) { //-- found </a>
$anchor_level--;
}
elseif (preg_match("#<($blocked_tags)>#i", $token)) { //-- found excluded tags
$tag_level++;
}
elseif (preg_match("#<\/($blocked_tags)>#i", $token)) { //-- found excluded closing tags
$tag_level--;
}
}
$filtered .= $token; //-- this token has now been filtered
}