回首页 ◎ 设为首页  
◎ 收藏本站  
◎ 联系我们  
  首 页  网络资讯  教程资料  免费资源  建站指南  休闲娱乐  经典整站  访客留言  
  当前位置:首 页 >> 教程资料 >> 网站编程 >> PHP站内搜索代码
最 新 推 荐
PHP入门速成推荐
ASP错误代码推荐
热 门 排 行
PHP站内搜索代码
远程调用163网易相册..
万能播放器源代码[php..
5分钟编写一个ASP论坛
记录蜘蛛爬行的代码 ASP
CMS内容管理系统概述
通过PHP程序知道蜘蛛..
ASP常用数据库连接及..
UTF8下的中文PHP编程
谈网页编程PHP语言的..
值得收藏的一些ASP代码
网站生成静态页面,及..
用IP地址来统计访问人..
用PHP和MySQL保存和输..
PHP生成静态页面详解
ASP中FSO的神奇功能
最 近 更 新
正则表达式
GB2312转换UTF-8的文件
用PHP自动把纯文本转..
asp中可以保存参数值..
PHP对战ASP:这还值得..
怎样才能成为PHP高手..
用ASP实现在线文章翻..
禁用页面缓存的几种方..
关于Zend Optimizer
什么是Session?
广 告 位 置
站 内 搜 索
关键词

搜索方式

搜索范围

精确匹配
PHP站内搜索代码

来源:盛绿设计 等级:默认等级
发布于2006-09-06 10:17 被读125次 【字体:
原作者: uchinaboy
特点:无需mysql支持;速度快;无需设置路径,放在哪级目录下,就搜索该目录和子目录;可以搜索一切文本类型的文件;显示文件相关内容;关键词自动高亮显示。
修改内容:增加了自动分页和风格设置文件。

搜索框代码(请单独存为html文件,如果放在search.php相同目录下,无需修改):
<form method="post" action="search.php">
<input type="text" name="key" size=40 value="">
<input type="submit" value="检索"></form>
****************************************************

search.php


<?php

require ("search.inc");
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."<br>";
 $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 "<BR><BR><BR>". $newpath."<BR><BR><BR>";
get_msg( $path."/". $filename);
}
}
closedir( $handle);
return  $i;
}

function match_show( $key,  $msg,  $newpath,  $filename) {
global  $ar,  $i;
 $key = chop( $key);
if( $key) {  $check_type = preg_match("/\.html? $/",  $filename);
if( $check_type) { $title = getHtmlTitle( $msg);}
 $msg = preg_replace("/<style>.+<\/style>/is", "",  $msg);
 $msg = preg_replace("/<[^>]+>/", "",  $msg);
 $value = preg_match("/.* $key.*/i",  $msg,  $res);
if( $value) {

 $res[0] = preg_replace("/ $key/i", "<FONT COLOR=\"red\"> $key</FONT>",  $res[0]);
 $k =  $res[0];
 $k = strrchr( $k, "<FONT");
 $k = substr( $k, 1, 100);
 $k = "<FONT COLOR=\"red\"> $key< $k";
if( $title) { $m =  $title;} else { $m =  $filename;}
 $i++;
 $link =  $newpath;
 $ar[] = " $i.◆<a href=\" $link\"> $m</a><BR><BR>" . $k."<BR><br>";
}
}else {
echo "请输入关键词";
exit;
}
}

function getHtmlTitle( $msg) {

/* Locate where <TITLE> is located in html file. */
 $lBound = strpos( $msg, '<title>') + 7; //7 is the lengh of <TITLE>.

if ( $lBound < 1)
return;

/* Locate where </TITLE> is located in html file. */
 $uBound = strpos( $msg, '</title>',  $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(".");
if (empty( $page))  $page=1;
 $maxresult=( $page*20);
 $resultcount = count( $ar);
if ( $resultcount%20==0)  $maxpageno= $resultcount/20;
else  $maxpageno=floor( $resultcount/20)+1;
if ( $page> $maxpageno) {  $page= $maxpageno;  $pagemax= $resultcount-1;  $pagemin=max
(0, $result_count-20);}
elseif ( $page==1) { $pagemin=0;  $pagemax=min( $result_count-1,20-1); }
else {  $pagemin=min( $resultcount-1,20*( $page-1));  $pagemax=min( $resultcount-
1, $pagemin+20-1); }
 $maxresult=min( $maxresult, $resultcount);
echo "<p align=\"center\">";
echo "检索结果";
echo "</p><hr>";
for ( $i=max(0, $maxresult-20);  $i< $maxresult;  $i++) {
print  $ar[ $i];
}
echo "<hr><p align=\"center\">";
echo " 已经搜索到了  $resultcount 条信息";
 $nextpage= $page+1;
 $previouspage= $page-1;
echo " --- [ <a href='search.php?key= $key&page= $nextpage' target='_self'>搜索下 20
个结果</a> ]";
echo " [ <a href='search.php?key= $key&page= $previouspage' target='_self'>返回上 20
个结果</a> ]";
exit;

?>
search.inc文件


<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<STYLE type=text/css>BODY {
font-family : 宋体;
font-size : 10.5pt;
font-weight : medium;
background : #CCCCFF;
position : absolute
}
P {
FONT-SIZE: 10.5pt
}
TABLE {
FONT-SIZE: 10.5pt
}
TD {
FONT-SIZE: 10.5pt; LETTER-SPACING: 1pt; LINE-HEIGHT: 15pt
}
A:LINK {
color : #000000;;TEXT-DECORATION: none

}
A:VISITED {
color : #000000;;TEXT-DECORATION: none

}
A:HOVER {
color : blue;TEXT-DECORATION: underline

}<!--
a {text-decoration: none}
--></style>
<title>全文检索</title>
<base target="_blank">
</head>

相关专题:暂无相关专题

上一篇:5分钟编写一个ASP论坛
下一篇:远程调用163网易相册的代码PHP/ASP版

共有评论 0 条 网友评分 0分 查看全部

【发表评论】 评分:1分 2分 3分 4分 5分


Powered By Www.Xydw.COM Ver1.14 管理
Copyright © 2004-2005 盛绿设计 All Right Reserved. XCMS
冀ICP备06026128号