文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>用PHP&XML编制迷你搜索引擎(四)

用PHP&XML编制迷你搜索引擎(四)

时间:2007-02-17  来源:PHP爱好者

[程序实例 ] 用PHP&XML编制迷你搜索引擎(四) 
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
作者    来源   
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
时间   2000年11月30日  阅读次数   247 
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
版本   -----  价值   17 
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
[投他一票] 
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
来源:奥索网
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
用PHP&XML编制迷你搜索引擎(四)
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
五、mini的搜索引擎
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
作了如干的铺垫,令人激动的时刻到来了。
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
分页等版式输出和用SQL的搜索引擎差不多,我就不加注释了。
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
第一段为仿sina,yahoo的按照类别查询
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
第二段为搜索查询部分(其实就是把整个树遍历一遍)显示符合的
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
内容。
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
说明:
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
"网络狂飙之谜你搜索引擎"适用于小数据量的数据索引查询,根据测试,当数据量超过1000条以后便不再适用,相当浪费主机资源,建议您超过400条记录采用mysql等数据库语言构建。希望您通过我的程序,对PHP对XML的解析函数有所认识。所有范例程序欢迎大家适用,如果您要将其用于自己的主页中请于我联系说明(其实就是将其用于的网站的名称告知于我)。
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
sfs([email protected])
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
以上的所有范例请到我的主页fire.oso.com.cn
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
上下载,我学PHP不到一个月,也刚来oso,希望文章中的错误之处大家能谅解。以后,我将为大家奉献出更多的源创范例。
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
废话少说,尽请看来。
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
__________________________________________________________
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
xml2.php
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
<html>
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
<body>
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
<style type=text/css>
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
td,p,li,input,select {font-size:12px;}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
A:link {font-size:12px;color:#00007f;}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
A:visited {font-size:12px;color:#00007f;}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
A:active {font-size:12px;color:#ff0000;}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
A:hover {font-size:12px;color:#ff0000;}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
.title {font-family:Tahoma; width=420 ;font-size :16px; font-weight :bold; color :steelblue; filter:Shadow(color="LightGrey", Direction="130");}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
.counter{font-family:Tahoma; color=green; font-size : 12px;}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
</style>
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
<?
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
// XML文件
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$file = "demo.xml";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$pagecount = 10;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
class Cweb { //网页
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
var $name;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
var $url;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
var $memo;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
class Cwebs {
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
var $items = array(Cweb);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
var $count = 0;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
class Csub { //类别
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
var $name;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
var $url;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
class Csubs {
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
var $items = array(Csub);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
var $count = 0;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
function xml_parse_from_file($parser, $file)
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
{
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if(!file_exists($file))
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
die("Can’t find file "$file".");
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if(!($fp = @fopen($file, "r")))
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
die("Can’t open file "$file".");
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
while($data = fread($fp, 4096)) {
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if(!xml_parse($parser, $data, feof($fp)))
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
return(false);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
fclose($fp);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
return(true);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
function start_element($parser, $name, $attrs)
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
{
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
global $show,$level,$levelcount,$maxlevel,$hide,$lev,$num,$PHP_SELF;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
global $webs,$subs;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$level += 1;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if($level>$maxlevel)$maxlevel=$level;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$levelcount[$level]+=1;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if($hide){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if($level==$lev&&$levelcount[$level]==$num)$hide=FALSE;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}else{
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if($level<=$lev)$hide=TRUE;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if(!$hide){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
switch($name){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
case "sub":
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$show="sub";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
case "web":
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$show="web";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
default:
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if($level==$lev+1&&$level>0){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
switch($show){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
case "sub":
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$subs->count+=1;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$subs->items[$subs->count]->url = "$PHP_SELF?lev=$level&num=$levelcount[$level]";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
case "web":
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$webs->count+=1;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
while ( list( $key, $val ) = each( $attrs ) ) {
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
switch(trim($key)){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
case "url" :
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$webs->items[$webs->count]->url=trim($val);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
case "memo" :
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$webs->items[$webs->count]->memo=trim($val);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
default:
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
function stop_element($parser, $name)
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
{
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
global $level;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$level -= 1;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
function data($parser, $data)
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
{
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
global $level,$hide,$show,$lev,$levelcount,$num;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
global $webs,$subs,$title;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if($level==$lev&&$levelcount[$level]==$num&&trim($data)!="")$title=trim($data);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if(!$hide)
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if(trim($data)!=""&&($level==$lev+1&&$level>0)){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
switch($show){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
case "sub":
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$subs->items[$subs->count]->name=trim($data);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
case "web":
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$webs->items[$webs->count]->name=trim($data);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
//main start
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
global $lev,$num,$PHP_SELF;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
global $title,$webs,$subs;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$level = -1;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$hide = TRUE;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$webs = new Cwebs;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$subs = new Csubs;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if($lev==""){$lev=0;$num=1;}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if($page=="")$page=0;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$parser = xml_parser_create();
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
xml_set_element_handler($parser, "start_element", "stop_element");
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
xml_set_character_data_handler($parser, "data");
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$ret = xml_parse_from_file($parser, $file);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if(!$ret)
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
{
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
die(sprintf("XML error: %s at line %d",
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
xml_error_string(xml_get_error_code($parser)),
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
xml_get_current_line_number($parser)));
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
xml_parser_free($parser);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<p><a href=$PHP_SELF>搜索首页</a> ";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<a href="javascript:history.back()">后退</a><form action="xml3.php" method="get">".
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
"<input name="search" type=edit><input type=submit value="Search"></form></p>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<center><div class=title>".$title."</div></center>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$i=0;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<table width="740">";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
while($i<$subs->count){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$i+=1;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if(($i%5)==1)echo"<tr>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<td width = 20%>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<a href=".$subs->items[$i]->url.">".$subs->items[$i]->name."</a>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "</td>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "</table><hr>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$i=$page*$pagecount;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if ($webs->count > 0){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<center>".($page+1)."/";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo Ceil($webs->count/$pagecount);}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<table width=740>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
while($i<$webs->count&&$i<(($page+1)*$pagecount)){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$i+=1;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<tr>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<td width=40>".$i."<td width=700><a href=".$webs->items[$i]->url.">".
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$webs->items[$i]->name."</tr><tr><td/><td>".$webs->items[$i]->memo;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "</td></tr>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "</table>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if ($webs->count > 0){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if($page>0)echo "<a href="."$PHP_SELF?lev=$lev&num=$num&page=".($page-1).">上一页</a> ";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if(($page+1)<Ceil($webs->count/$pagecount))echo "<a href="."$PHP_SELF?lev=$lev&num=$num&page=".($page+1).">下一页</a>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
?>
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
</body>
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
</html>
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
__________________________________________________________
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
xml3.php
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
关键字匹配采用eregi函数,功能相对简单,且有些bug,
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
如输入C++会报错(因为+是关键字)这点就不如用SQL查询了。
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
您可以在下面的程序的基础上加以完善,我这个迷你的就
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
起到抛砖引玉的作用吧。
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
<html>
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
<body>
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
<style type=text/css>
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
td,p,li,input,select {font-size:12px;}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
A:link {font-size:12px;color:#00007f;}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
A:visited {font-size:12px;color:#00007f;}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
A:active {font-size:12px;color:#ff0000;}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
A:hover {font-size:12px;color:#ff0000;}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
.title {font-family:Tahoma; width=420 ;font-size :16px; font-weight :bold; color :steelblue; filter:Shadow(color="LightGrey", Direction="130");}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
.counter{font-family:Tahoma; color=green; font-size : 12px;}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
</style>
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
<?
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
// XML文件
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$file = "demo.xml";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$pagecount = 10;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
class Cweb { //网页
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
var $name;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
var $url;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
var $memo;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
class Cwebs {
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
var $items = array(Cweb);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
var $count = 0;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
class Csub { //类别
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
var $name;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
var $url;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
class Csubs {
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
var $items = array(Csub);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
var $count = 0;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
// 解析XML文件的函数
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
function xml_parse_from_file($parser, $file)
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
{
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if(!file_exists($file))
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
die("Can’t find file "$file".");
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if(!($fp = @fopen($file, "r")))
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
die("Can’t open file "$file".");
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
while($data = fread($fp, 4096)) {
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if(!xml_parse($parser, $data, feof($fp)))
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
return(false);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
fclose($fp);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
return(true);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
function start_element($parser, $name, $attrs)
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
{
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
global $show,$level,$levelcount,$maxlevel,$PHP_SELF;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
global $webs,$subs;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
global $search,$finded;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$finded=FALSE;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$level += 1;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if($level>$maxlevel)$maxlevel=$level;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$levelcount[$level]+=1;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
switch($name){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
case "sub":
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$show="sub";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
case "web":
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$show="web";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
default:
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
switch($show){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
case "sub":
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$subs->count+=1;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$subs->items[$subs->count]->url = "xml2.php?lev=$level&num=$levelcount[$level]";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
case "web":
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$webs->count+=1;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
while ( list( $key, $val ) = each( $attrs ) ) {
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if(eregi($search,$val))$finded=TRUE;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
switch(trim($key)){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
case "url" :
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$webs->items[$webs->count]->url=trim($val);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
case "memo" :
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$webs->items[$webs->count]->memo=trim($val);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
default:
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
function stop_element($parser, $name)
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
{
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
global $level;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$level -= 1;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
function data($parser, $data)
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
{
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
global $level,$show,$levelcount;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
global $webs,$subs;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
global $search,$finded;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if(trim($data)!=""){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
switch($show){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
case "sub":
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$subs->items[$subs->count]->name=trim($data);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if(!eregi($search,$data))$subs->count-=1;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
case "web":
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$webs->items[$webs->count]->name=trim($data);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if((!eregi($search,$data))&&(!$finded))$webs->count-=1;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
break;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
//main start
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
global $PHP_SELF;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
global $search,$webs,$subs;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$level = -1;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$hide = TRUE;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$webs = new Cwebs;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$subs = new Csubs;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if($page=="")$page=0;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if($search=="")$search="请输入关键字";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$parser = xml_parser_create();
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
xml_set_element_handler($parser, "start_element", "stop_element");
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
xml_set_character_data_handler($parser, "data");
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$ret = xml_parse_from_file($parser, $file);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if(!$ret)
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
{
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
die(sprintf("XML error: %s at line %d",
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
xml_error_string(xml_get_error_code($parser)),
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
xml_get_current_line_number($parser)));
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
xml_parser_free($parser);
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
// 输出
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<p><a href=xml2.php>搜索首页</a> ";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<a href="javascript:history.back()">后退</a></p>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<center><div class=title>".$search."</div></center>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$i=0;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<table width="740">";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
while($i<$subs->count){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$i+=1;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if(($i%5)==1)echo"<tr>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<td width = 20%>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<a href=".$subs->items[$i]->url.">".$subs->items[$i]->name."</a>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "</td>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "</table><hr>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF

chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$i=$page*$pagecount;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if ($webs->count > 0){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<center>".($page+1)."/";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo Ceil($webs->count/$pagecount);}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<table width=740>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
while($i<$webs->count&&$i<(($page+1)*$pagecount)){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$i+=1;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<tr>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "<td width=40>".$i."<td width=700><a href=".$webs->items[$i]->url.">".
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
$webs->items[$i]->name."</tr><tr><td/><td>".$webs->items[$i]->memo;
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "</td></tr>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
echo "</table>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if ($webs->count > 0){
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if($page>0)echo "<a href="."$PHP_SELF?search=$search&page=".($page-1).">上一页</a> ";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
if(($page+1)<Ceil($webs->count/$pagecount))echo "<a href="."$PHP_SELF?search=$search&page=".($page+1).">下一页</a>";
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
}
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
?>
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
</body>
chi na it p owe r .commPCVOSX9gAusXwuFRNSA4TUUF
</html>
php爱好者站 http://www.phpfans.net Linux|Apache|IIS.
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载