Добавил функцию
function fixEncoding($s, $encoding = 'UTF-8') {
$s = @iconv('UTF-16', $encoding . '//IGNORE', iconv($encoding, 'UTF-16//IGNORE', $s));
return str_replace("\xEF\xBB\xBF", '', $s);
}
в твой код
function summarize($haystack, $needle, $wordLimit = 5) {
$haystack = fixEncoding($haystack);
$needle = fixEncoding($needle);
// first get summary of text around key word (needle)
$preg_safe = str_replace(" ", "\s", preg_quote($needle));
$pattern = "/(\w*\S\s+){0,$wordLimit}\S*\b($preg_safe)\b\S*(\s\S+){0,$wordLimit}/iux";
if (preg_match_all($pattern, $haystack, $matches)) {
$summary = str_replace(strtolower($needle), "<strong>$needle</strong>", $matches[0][0]) . '…';
} else {
$summary = false;
}
return $summary;
}
Работает