this would to work with any language, i hope.
tested on czech (eastern europe) lang.
<?php
function safeHighlight( $needleArr, $shearLft, $shearRgt, $subject, $encoding = 'utf-8')
{
$e = $encoding;
if( !is_array( $needleArr))
return $subject;
$nA = array_values(
array_unique(
array_diff( $needleArr, array(''))
)
);
if( !($nC = count( $nA)))
return $subject; if( !(($rLL = mb_strlen( $rL = $shearLft, $e))
+ ($rRL = mb_strlen( $rR = $shearRgt, $e))))
return $subject; if( !($sL = mb_strlen( $s = $subject, $e)))
return null; $sW = mb_strtolower( $s, $e);
$m = str_repeat( '0', $sL);
for( $n=0; $n<$nC; $n++)
{
$nW = mb_strtolower( $nA[ $n], $e);
$o = 0; $nL = mb_strlen( $nW, $e); while( false !== ($p = mb_strpos( $sW, $nW, $o, $e)))
{
for( $q=$p; $q<($p+$nL); $q++)
if( $m[ $q])
{
$o+= $nL;
continue 2;
}
$sE[0] = mb_substr( $s, 0, $p, $e);
$sE[1] = mb_substr( $s, $p, $nL, $e);
$sE[2] = mb_substr( $s, $p+$nL, $sL-$p-$nL, $e);
$mE[0] = mb_substr( $m, 0, $p, $e);
$mE[1] = mb_substr( $m, $p+$nL, $sL-$p-$nL, $e);
$sE[1] = $rL.$sE[1].$rR;
$sL+= $rLL + $rRL;
$m = $mE[0] . str_repeat( '1', $rLL + $nL + $rRL) . $mE[1];
$s = implode( $sE);
$sW = mb_strtolower( $s, $e);
$o+= $rLL + $nL + $rRL;
if( $o>=$sL)
break;
} } return $s;
} ?>