$length)
{
$string = wordwrap($string, $length);
$string = $mb? mb_substr($string, 0, mb_strpos($string, "\n")) : substr($string, 0, strpos($string, "\n"));
}
return $string;
}
function clip_string($string, $pos, $length, $total) {
global $mb;
if($length) {
$m = $mb ? mb_substr($string, $pos, $length) : substr($string, $pos, $length);
$before = $mb ? mb_substr($string, 0, $pos) : substr($string, 0, $pos);
$after = $mb ? mb_substr($string, $pos + $length, mb_strlen($string) - ($pos + $length)) : substr($string, $pos + $length, strlen($string) - ($pos + $length));
$before = rev_string($before);
if($total < strlen($string)) {
$half = intval(($total - $length) / 2);
} else {
$half = $total;
}
$hlPre = '';
$hlPost = '';
$out = htmlentities(rev_string(word_trunc($before, $half))) . $hlPre . htmlentities($m) . $hlPost . htmlentities(word_trunc($after, $half));
return $out;
} else {
return htmlentities(word_trunc($string, $total));
}
}
if(count($searchResults) == 0) {
$result = '
';
$result = str_replace('{title}', htmlentities('Nessun risultato'), $result);
$result = str_replace('{text}', '', $result);
echo $result;
}
else {
echo '';
foreach($searchResults as $searchResult) {
$result = '
';
$result = str_replace('{title}', '
' . $searchResult['title'] . '', $result);
$text = "";
foreach($searchResult['snippets'] as $s) {
$pos = $s['pos'];
$m = $s['matchlen'];
$snippet = clip_string($s['text'], $pos, $m, $s['snip']);
if(strlen($text))
$text .= " … ";
$text .= " " . $snippet;
}
$result = str_replace('{text}', $text, $result);
echo $result;
}
}
?>