有一個靜態的舊網站,在HTML上有50頁。問題是如何實現一個不太快的搜索?哪種方式看?我也做了一個PHP腳本,它只是搜索文件中的文本,但它的工作原理很慢,有一些索引頁面的方法或類似的東西。如何在靜態網站中進行搜索?
<?php
ini_set('max_execution_time', 900);
if(!isset($_GET['s'])) {
die('You must define a search term!');
}
$search_in = array('html', 'htm');
$search_dir = '.';
$countWords = 15;
$files = list_files($search_dir);
$search_results = array();
foreach($files as $file){
$contents = file_get_contents($file);
preg_match_all("/\<p\>(.*)".$_GET['s']."(.*)\<\/p\>/i", $contents, $matches, PREG_SET_ORDER);
foreach($matches as $match){
$match[1] = trim_result($match[1]);
$match[2] = trim_result($match[2], true);
$match[1] .= '<span style="background: #ffff00;">';
$match[2] = '</span>'.$match[2];
preg_match("/\<title\>(.*)\<\/title\>/", $contents, $matches2);
$search_results[] = array($file, $match[1].$_GET['s'].$match[2], $matches2[1]);
}
}
?>
<html>
<head>
<title>Search results</title>
</head>
<body>
<?php foreach($search_results as $result) :?>
<div>
<h3><a href="<?php echo $result[0]; ?>"><?php echo $result[2]; ?></a></h3>
<p><?php echo $result[1]; ?></p>
</div>
<?php endforeach; ?>
</body>
</html>
<?php
function list_files($dir){
global $search_in;
$result = array();
if(is_dir($dir)){
if($dh = opendir($dir)){
while (($file = readdir($dh)) !== false) {
if(!($file == '.' || $file == '..')){
$file = $dir.'/'.$file;
if(is_dir($file) && $file != './.' && $file != './..'){
$result = array_merge($result, list_files($file));
}
else if(!is_dir($file)){
if(in_array(get_file_extension($file), $search_in)){
$result[] = $file;
}
}
}
}
}
}
return $result;
}
function get_file_extension($filename){
$result = '';
$parts = explode('.', $filename);
if(is_array($parts) && count($parts) > 1){
$result = end($parts);
}
return $result;
}
function trim_result($text, $start = false){
$words = split(' ', strip_tags($text));
if($start){
$words = array_slice($words, 0, $countWords);
}
else{
$start = count($words) - $countWords;
$words = array_slice($words, ($start < 0 ? 0 : $start), $countWords);
}
return implode(' ', $words);
}
?>