php全站文章搜索,翻出一篇老文章:php文本站内全文检索-PHP教程,PHP应用

/****************************************************

原作者: uchinaboy

修改:lingshidao

特点:无需mysql支持;速度快;无需设置路径,放在哪级目录下,就搜索该目录和子目录;可以搜索一切文本类型的文件;显示文件相关内容;关键词自动高亮显示。

修改内容:增加了自动分页和风格设置文件。

搜索框代码(如果放在search.php相同目录下,无需修改):

****************************************************/

require ("template.php");

echo "

";

echo "检索结果";

echo "


";

if (function_exists("set_time_limit") && !get_cfg_var(safe_mode)){

set_time_limit(600);}

function get_msg($path) {

global $key, $i;

$handle = opendir($path);

while ($filename = readdir($handle)) {

//echo $path."/".$filename."
";

$newpath = $path."/".$filename;

if (is_file($newpath)) {

$fp = fopen($newpath, "r");

$msg = fread($fp, filesize($newpath));

fclose($fp);

match_show($key, $msg, $newpath, $filename);

}

if (is_dir($path."/".$filename) && ($filename != ".") &&  ($filename != "..")) {

//echo "
".$newpath."
";

get_msg($path."/".$filename);

}

}

closedir($handle);

return $i;

}

function match_show($key, $msg, $newpath, $filename) {

global $i;

$key = chop($key);

if($key) { $check_type = preg_match("/\.html?$/", $filename);

if($check_type) {$title = gethtmltitle($msg);}

$msg = preg_replace("/

$msg = preg_replace("/<[^>]+>/", "", $msg);

$value = preg_match("/.*$key.*/i", $msg, $res);

if($value) {

if($title) {$m = $title;} else {$m = $filename;}

$i++;

$link = $newpath;

echo "$i.◆$m

";

}

}else {

echo "请输入关键词";

exit;

}

}

function gethtmltitle($msg) {

/* locate where

is located in html file. */

$lbound = strpos($msg,

) + 7; //7 is the lengh of .

if ($lbound < 1)

return;

/* locate where

is located in html file. */

$ubound = strpos($msg,

, $lbound);

if ($ubound < $lbound)

return;

/* clean html and php tags out of $title with the madness below. */

$title = ereg_replace("[\t\n\r]", , substr($msg, $lbound, $ubound – $lbound));

$title = trim(strip_tags($title));

if (strlen($title) < 1) //a blank title is worthless.

return;

return $title;

}

$i = get_msg(".");

echo "


";

echo " 已经搜索到了 $i 条信息";

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值