Author: Jorge Maldonado Ventura
Date: 2017-04-22 20:38
Modified: 2018-01-11 16:38
Save_as: buscar.php
Status: hidden
Title: Resultados
' . $word . '
', $page['text'], -1, $match_count);
}
if ($match_count > 0) {
$score += 10 * $match_count;
}
}
if ($score != 0) {
$found_results[] = ['score' => $score, 'title' => $page['title'], 'url' => $page['url'], 'text' => $page['text']];
}
}
/**
* Compare results score.
*/
function comp_result_score($a, $b) {
if ($a['score'] == $b['score']) {
return 0;
}
return ($a['score'] > $b['score']) ? -1 : 1;
}
if ($stop_words_ignored) {
printf('
%s
', 'Las palabras comunes se ignoran en gran parte');
}
$found_results_count = count($found_results);
if ($found_results_count > 0) {
usort($found_results, 'comp_result_score');
if ($found_results_count == 1) {
$found_results_count_str = '1 resultado';
} else {
$found_results_count_str = $found_results_count . ' resultados';
}
} else if ($found_results_count == 0) {
$found_results_count_str = NULL;
printf('
%s
', 'No se ha encontrado nada');
}
printf('
%s
', $found_results_count_str);
foreach ($found_results as $found_result) {
printf('
%s
', $found_result['url'], $found_result['title']);
printf('
%s
', $found_result['url'], $found_result['url']);
$description_words = explode(' ', $found_result['text']);
$description_words_count = count($description_words);
$first_match = false;
for ($i = 0; $i < $description_words_count; $i++) {
if ($description_words[$i] == '
') {
$first_match = $i;
break;
}
}
if ($first_match !== false) {
echo '';
if ($first_match - 12 <= 0) {
for ($i = 0; $i < $DESCRIPTION_LENGTH; $i++) {
echo $description_words[$i] . ' ';
}
echo '...';
} else if($first_match + 12 >= $description_words_count) {
echo '...';
for ($i = $first_match - 12; $i < $description_words_count; $i++) {
echo ' ' . $description_words[$i];
}
}
else {
echo '...';
for ($i = $first_match - 12; $i <= $first_match + 12; $i++) {
echo $description_words[$i] . ' ';
}
echo '...';
}
echo '
';
} elseif ( $description_words_count === 1 ) {
printf('', 'No hay palabras en el artículo');
break;
} else {
echo '';
for ($i = 0; $i < $DESCRIPTION_LENGTH; $i++) {
echo $description_words[$i] . ' ';
}
if ($description_words_count > $DESCRIPTION_LENGTH) {
echo '...';
}
echo '
';
}
}
} else {
printf('%s
', 'Aún no has buscado');
}
?>