Salut,
Comment limiter le nombre de caractères à afficher dans le résultat d'une recherche tout en faisant apparaitre le mot recherché. Jusqu'ici je n'ai réussi qu'à différencier le mot recherché. J'ai besoin de votre aide.
<?php
// find the locations of each of the words
// Nothing exciting here. The array_unique is required
// unless you decide to make the words unique before passing in
function _extractLocations($words, $fulltext) {
$locations = array();
foreach($words[0] as $word) {
$wordlen = strlen($word);
$loc = stripos($fulltext, $word);
while($loc !== FALSE) {
$locations[] = $loc;
$loc = stripos($fulltext, $word, $loc + $wordlen);
}
}
$locations = array_unique($locations);
sort($locations);
return $locations;
}
// Work out which is the most relevant portion to display
// This is done by looping over each match and finding the smallest distance between two found
// strings. The idea being that the closer the terms are the better match the snippet would be.
// When checking for matches we only change the location if there is a better match.
// The only exception is where we have only two matches in which case we just take the
// first as will be equally distant.
function _determineSnipLocation($locations, $prevcount) {
// If we only have 1 match we dont actually do the for loop so set to the first
$startpos = $locations[0];
$loccount = count($locations);
$smallestdiff = PHP_INT_MAX;
// If we only have 2 skip as its probably equally relevant
if(count($locations) > 2) {
// skip the first as we check 1 behind
for($i=1; $i < $loccount; $i++) {
if($i == $loccount-1) { // at the end
$diff = $locations[$i] - $locations[$i-1];
}
else {
$diff = $locations[$i+1] - $locations[$i];
}
if($smallestdiff > $diff) {
$smallestdiff = $diff;
$startpos = $locations[$i];
}
}
}
$startpos = $startpos > $prevcount ? $startpos - $prevcount : 0;
return $startpos;
}
// 1/6 ratio on prevcount tends to work pretty well and puts the terms
// in the middle of the extract
function extractRelevant($words, $fulltext, $rellength=300, $prevcount=50, $indicator='...') {
$textlength = strlen($fulltext);
if($textlength <= $rellength) {
return $fulltext;
}
$locations = _extractLocations($words, $fulltext);
$startpos = _determineSnipLocation($locations,$prevcount);
// if we are going to snip too much...
if($textlength-$startpos < $rellength) {
$startpos = $startpos - ($textlength-$startpos)/2;
}
$reltext = substr($fulltext, $startpos, $rellength);
// check to ensure we dont snip the last word if thats the match
if( $startpos + $rellength < $textlength) {
$reltext = substr($reltext, 0, strrpos($reltext, " ")).$indicator; // remove last word
}
// If we trimmed from the front add ...
if($startpos != 0) {
$reltext = $indicator.substr($reltext, strpos($reltext, " ") + 1); // remove first word
}
return $reltext;
}
$fulltext="Bienvenue sur Yahoo !, la page d'accueil la plus visitée au monde. Trouvez rapidement ce que vous cherchez, contactez des amis et restez au courant des dernières nouvelles et informations. CloudSponge fournit une interface permettant à vos utilisateurs d'importer facilement des contacts à partir des divers services de messagerie Web les plus populaires, notamment Yahoo, Gmail et Hotmail / MSN, ainsi que des carnets d'adresses de bureau courants, tels que Carnet d'adresses Mac et Outlook.";
$words="Yahoo";
_extractLocations($words, $fulltext);
_determineSnipLocation();
extractRelevant($words, $fulltext);
?>
Quand je valide j'ai deux erreurs :
echo extractRelevant($words, $fulltext); et voici la réponse est: Warning: Invalid argument supplied for foreach() in .... on line 5 et Notice: Undefined offset: 0 in .... on line 27 et ceci : Since its launch in November 2007, Android has not only dramatically increased consumer choice but also improved the entire mobile experience for users. Today, more than 150 million Android devices have been activated worldwide with over 550,000 devices now lit up every day through a network of...Je t'accorde que la doc est très succinte et n'a pas d'exemple. mais @rthur a mis le doigt dessus et toi tu as modifié le code de _extractLocations donc 2 modifications sont necéssaire.Le 1er, c'est probablement car ta variable $words doit être un tableau avec plusieurs mots.