1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
<?php
// +------ Fonction pour rechercher les keywords dans le texte et les afficher selon une position -------+
function get_snippet($keywordsy, $texte)
{
$snippet='';
$span = 140;
$strlen_max = 425;
$keywordsy = RemoveLess($keywordsy); // appel de la 1ère fonction
$keywordsy = preg_replace("#[\\\/:\*\?\(\)\%\&\+\#\"<>'\|]#ius", " ", $keywordsy) ; // Supprimer les caractères spéciaux
$keywordsy = trim(preg_replace("#( [[:alnum:]]{1,3} )#Ui", " ", " ".$keywordsy." ")); // ne garde que + de 3 caractères et ajout d'un espace avant et après
$texte = html_entity_decode("$texte", ENT_QUOTES); // Traiter le texte qui s'affiche selon les charsets
$words = join('|', explode(' ', preg_quote($keywordsy)));
preg_match_all("#(\W.{0,$span}\W)($words)(\W.{0,$span}\W)#ius", " $texte ", $matches);
foreach($matches[0] as $match)
{
if (!$match = trim($match)) continue;
if (isset($snippet)) $snippet .= "$match.. "; else $snippet = "...$match...";
if (strlen($snippet.htmlspecialchars($match[0], 'UTF-8').".. ") > $strlen_max) break;
}
$snippet = preg_replace("#($words)#iu", '<b>$1</b>', $snippet);
return $snippet;
}
?> |
Partager