Salut. Le premier lien n'a pas de problème. Dans le second, il y a trois fonctions t voici mon code:
<?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 :
1.Warning: Invalid argument supplied for foreach() in C:\wamp\www\monsite.cg\new 1.php on line 8
2. Fatal error: Uncaught ArgumentCountError: Too few arguments to function _determineSnipLocation(), 0 passed in C:\wamp\www\cnss.cg\new 1.php on line 91 and exactly 2 expected in C:\wamp\www\monsite.cg\new 1.php on line 28
ArgumentCountError: Too few arguments to function _determineSnipLocation(), 0 passed in C:\wamp\www\monsite.cg\new 1.php on line 91 and exactly 2 expected in C:\wamp\www\monsite.cg\new 1.php on line 28
J'ai besoin de votre aide, merci