. HTML-, :
<form method="post" action="[_LINK_HELP_SEARCH_]">
<div class="static-text">(_INTRO_)</div>
<input class="inline" name="q" id="search" type="text" value="[_QUERY_]" />
<input class="inline" type="submit" value="(_SEARCH_)" />
<div class="micro-text">(_EXAMPLE_)</div>
</form>
. [XXX] (YYY) , .
, PHP :
$query = preg_replace('/\s{2,}/', ' ', $query);
$words = explode(' ', $query);
(
$help_files = _get_all_files('help');
$help_files = array_slice($help_files, 0, MAX_RESULTS);
foreach($help_files as $file) {
, "help", . , _get_all_files - , PHP .
:
$text_file = '';
$filename = $file['page'];
if (_file_exists($filename)) {
$text_file = _read_php_file($filename);
}
$text_file = strtolower($text_file);
$text_file = strip_tags($text_file);
$text_file = preg_replace('/\[_(.*?)_\]/', '...', $text_file);
$text_file = preg_replace(array('/\s{2,}/', '[\t\n]'), ' ', $text_file);
, _read_php_file PHP, .. , , . , , HTML . HTML, readfile() .
, :
$score = 0;
foreach ($words as $word) {
if (strpos($text_file, $word) !== false) {
$score++;
}
}
, , . , , , .
:
$pos = strpos($text_file, $words[0]);
$cut_ini = max($pos - RESUME_LIMIT/2, 0);
$extract = substr($text_file, $cut_ini, RESUME_LIMIT);
$extract = "...$extract...";
, , ( ), :
if (($score > 0) && (count($words) / $score > 0.7)) {
$result = array (
'extract' => $extract,
'title' => $file['title'],
'link' => $file['page'],
'score' => $score
);
$results[] = $result;
}
, , , :
usort($results, "_search_sort");
:
function _search_sort($a, $b) {
if ($a['score'] == $b['score']) {
return 0;
}
return ($a['score'] > $b['score']) ? -1 : 1;
}
. , .