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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| <?php
//$keywordsy = "domaine rumantsch";
//$keywordsy = "doma* rumantsch";
$keywordsy = "doma italiano";
$texte = "Google recherche images maps play youtube actualités gmail drive plus » historique web | paramètres | connexion × surfez encore plus vite installer google chrome suisse recherche avancée outils linguistiques le domaine google.ch est disponible en : deutsch english italiano rumantsch solutions publicitaires solutions deutsch english italiano rumantsch solutions domaines publicitaires";
// +------ Fonction pour supprimer les keywords de 3 lettres et moins -------+
function RemoveLess($String,$Char=3)
{
$StringArray=explode (" ",$String);
foreach ($StringArray as &$Word)
{
if (mb_strlen($Word,"UTF-8")>$Char)
{
$Str.=$Word." ";
}
}
return trim($Str);
}
// +------ Fonction pour rechercher les keywords dans le texte et les afficher selon une position + coloration -------+
function get_snippet($keywordsy, $texte) {
$snippet='';
$span = 130;
$strlen_max = 400;
$keywordsy = RemoveLess($keywordsy); // appel de la 1ère fonction
$keywordsy = trim(preg_replace("#( [[:alnum:]]{1,3} )#Ui", " ", " ".$keywordsy." ")); // ajout d'un espace avant et
$words = join('|', explode(' ', preg_quote($keywordsy)));
preg_match_all("#(\W.{0,$span}\W)($words)(\W.{0,$span}\W)#iu", " $texte ", $matches);
//$s = '\s\x00-/:-@\[-`{-~'; //character set for start/end of words
//preg_match_all('#(?<=['.$s.']).{1,130}(('.$words.').{1,130})+(?=['.$s.'])#uis', $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;
}
// <!-- Appel de la fonction snippet -->
$description = get_snippet($keywordsy, $texte);
echo $description;
?> |
Partager