1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| function snippet_max($texte, $keywordsy, $strlen_max) {
//$keywordsy = str_replace(' ',' ',preg_replace("# [[:alnum:]]{1,3}[\.,;:]? #", "", str_replace(' ',' ',$keywordsy)));
while(preg_match("# ([[:alnum:]]{1,3}) #", $keywordsy))
$keywordsy = preg_replace("#( [[:alnum:]]{1,3} )#", " ", $keywordsy);
$words = join('|', explode(' ', preg_quote($keywordsy)));
//lookahead/behind assertions ensures cut between words
$s = '\s\x00-/:-@\[-`{-~'; //character set for start/end of words
preg_match_all('#(?<=['.$s.']).{1,100}(('.$words.').{1,100})+(?=['.$s.'])#uis', $texte, $matches, PREG_SET_ORDER);
$result = "";
foreach($matches as $line) {
if (strlen($result.htmlspecialchars($line[0], 0, 'UTF-8')."... ") > $strlen_max) break;
$result .= htmlspecialchars($line[0], 0, 'UTF-8')."... ";
}
//highlight
$result = preg_replace('#'.$words.'#iu', "<span class=\"highlight_word\">\$0</span>", $result);
return $result;
}
$description = snippet_max($texte, $keywordsy, 400); |
Partager