用PHP&XML编制迷你搜索引擎(四)_PHP
程序员文章站
2024-01-30 12:58:16
...
[程序实例 ] 用PHP&XML编制迷你搜索引擎(四)
作者 来源
时间 2000年11月30日 阅读次数 247
版本 ----- 价值 17
[投他一票]
来源:奥索网
用PHP&XML编制迷你搜索引擎(四)
五、mini的搜索引擎
作了如干的铺垫,令人激动的时刻到来了。
分页等版式输出和用SQL的搜索引擎差不多,我就不加注释了。
第一段为仿sina,yahoo的按照类别查询
第二段为搜索查询部分(其实就是把整个树遍历一遍)显示符合的
内容。
说明:
"网络狂飙之谜你搜索引擎"适用于小数据量的数据索引查询,根据测试,当数据量超过1000条以后便不再适用,相当浪费主机资源,建议您超过400条记录采用mysql等数据库语言构建。希望您通过我的程序,对PHP对XML的解析函数有所认识。所有范例程序欢迎大家适用,如果您要将其用于自己的主页中请于我联系说明(其实就是将其用于的网站的名称告知于我)。
sfs(sfsz@chinese.com)
以上的所有范例请到我的主页fire.oso.com.cn
上下载,我学PHP不到一个月,也刚来oso,希望文章中的错误之处大家能谅解。以后,我将为大家奉献出更多的源创范例。
废话少说,尽请看来。
__________________________________________________________
xml2.php
html>
body>
style type=text/css>
td,p,li,input,select {font-size:12px;}
A:link {font-size:12px;color:#00007f;}
A:visited {font-size:12px;color:#00007f;}
A:active {font-size:12px;color:#ff0000;}
A:hover {font-size:12px;color:#ff0000;}
.title {font-family:Tahoma; width=420 ;font-size :16px; font-weight :bold; color :steelblue; filter:Shadow(color="LightGrey", Direction="130");}
.counter{font-family:Tahoma; color=green; font-size : 12px;}
/style>
// XML文件
$file = "demo.xml";
$pagecount = 10;
class Cweb { //网页
var $name;
var $url;
var $memo;
}
class Cwebs {
var $items = array(Cweb);
var $count = 0;
}
class Csub { //类别
var $name;
var $url;
}
class Csubs {
var $items = array(Csub);
var $count = 0;
}
function xml_parse_from_file($parser, $file)
{
if(!file_exists($file))
die("Can’t find file "$file".");
if(!($fp = @fopen($file, "r")))
die("Can’t open file "$file".");
while($data = fread($fp, 4096)) {
if(!xml_parse($parser, $data, feof($fp)))
return(false);
}
fclose($fp);
return(true);
}
function start_element($parser, $name, $attrs)
{
global $show,$level,$levelcount,$maxlevel,$hide,$lev,$num,$PHP_SELF;
global $webs,$subs;
$level += 1;
if($level>$maxlevel)$maxlevel=$level;
$levelcount[$level]+=1;
if($hide){
if($level==$lev&&$levelcount[$level]==$num)$hide=FALSE;
}else{
if($level
}
if(!$hide){
switch($name){
case "sub":
$show="sub";
break;
case "web":
$show="web";
break;
default:
break;
}
if($level==$lev+1&&$level>0){
switch($show){
case "sub":
$subs->count+=1;
$subs->items[$subs->count]->url = "$PHP_SELF?lev=$level&num=$levelcount[$level]";
break;
case "web":
$webs->count+=1;
while ( list( $key, $val ) = each( $attrs ) ) {
switch(trim($key)){
case "url" :
$webs->items[$webs->count]->url=trim($val);
break;
case "memo" :
$webs->items[$webs->count]->memo=trim($val);
break;
}
}
break;
default:
break;
}
}
}
}
function stop_element($parser, $name)
{
global $level;
$level -= 1;
}
function data($parser, $data)
{
global $level,$hide,$show,$lev,$levelcount,$num;
global $webs,$subs,$title;
if($level==$lev&&$levelcount[$level]==$num&&trim($data)!="")$title=trim($data);
if(!$hide)
if(trim($data)!=""&&($level==$lev+1&&$level>0)){
switch($show){
case "sub":
$subs->items[$subs->count]->name=trim($data);
break;
case "web":
$webs->items[$webs->count]->name=trim($data);
break;
}
}
}
//main start
global $lev,$num,$PHP_SELF;
global $title,$webs,$subs;
$level = -1;
$hide = TRUE;
$webs = new Cwebs;
$subs = new Csubs;
if($lev==""){$lev=0;$num=1;}
if($page=="")$page=0;
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_element", "stop_element");
xml_set_character_data_handler($parser, "data");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
$ret = xml_parse_from_file($parser, $file);
if(!$ret)
{
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
xml_parser_free($parser);
echo " ";
echo " ";
$i=0;
echo "";
";
$i=$page*$pagecount;
if ($webs->count > 0){
echo "".($page+1)."/";
echo Ceil($webs->count/$pagecount);}
echo "";
";
if ($webs->count > 0){
if($page>0)echo "上一页 ";
if(($page+1)count/$pagecount))echo "下一页";
}
?>
/body>
/html>
__________________________________________________________
xml3.php
关键字匹配采用eregi函数,功能相对简单,且有些bug,
如输入C++会报错(因为+是关键字)这点就不如用SQL查询了。
您可以在下面的程序的基础上加以完善,我这个迷你的就
起到抛砖引玉的作用吧。
html>
body>
style type=text/css>
td,p,li,input,select {font-size:12px;}
A:link {font-size:12px;color:#00007f;}
A:visited {font-size:12px;color:#00007f;}
A:active {font-size:12px;color:#ff0000;}
A:hover {font-size:12px;color:#ff0000;}
.title {font-family:Tahoma; width=420 ;font-size :16px; font-weight :bold; color :steelblue; filter:Shadow(color="LightGrey", Direction="130");}
.counter{font-family:Tahoma; color=green; font-size : 12px;}
/style>
// XML文件
$file = "demo.xml";
$pagecount = 10;
class Cweb { //网页
var $name;
var $url;
var $memo;
}
class Cwebs {
var $items = array(Cweb);
var $count = 0;
}
class Csub { //类别
var $name;
var $url;
}
class Csubs {
var $items = array(Csub);
var $count = 0;
}
// 解析XML文件的函数
function xml_parse_from_file($parser, $file)
{
if(!file_exists($file))
die("Can’t find file "$file".");
if(!($fp = @fopen($file, "r")))
die("Can’t open file "$file".");
while($data = fread($fp, 4096)) {
if(!xml_parse($parser, $data, feof($fp)))
return(false);
}
fclose($fp);
return(true);
}
function start_element($parser, $name, $attrs)
{
global $show,$level,$levelcount,$maxlevel,$PHP_SELF;
global $webs,$subs;
global $search,$finded;
$finded=FALSE;
$level += 1;
if($level>$maxlevel)$maxlevel=$level;
$levelcount[$level]+=1;
switch($name){
case "sub":
$show="sub";
break;
case "web":
$show="web";
break;
default:
break;
}
switch($show){
case "sub":
$subs->count+=1;
$subs->items[$subs->count]->url = "xml2.php?lev=$level&num=$levelcount[$level]";
break;
case "web":
$webs->count+=1;
while ( list( $key, $val ) = each( $attrs ) ) {
if(eregi($search,$val))$finded=TRUE;
switch(trim($key)){
case "url" :
$webs->items[$webs->count]->url=trim($val);
break;
case "memo" :
$webs->items[$webs->count]->memo=trim($val);
break;
}
}
break;
default:
break;
}
}
function stop_element($parser, $name)
{
global $level;
$level -= 1;
}
function data($parser, $data)
{
global $level,$show,$levelcount;
global $webs,$subs;
global $search,$finded;
if(trim($data)!=""){
switch($show){
case "sub":
$subs->items[$subs->count]->name=trim($data);
if(!eregi($search,$data))$subs->count-=1;
break;
case "web":
$webs->items[$webs->count]->name=trim($data);
if((!eregi($search,$data))&&(!$finded))$webs->count-=1;
break;
}
}
}
//main start
global $PHP_SELF;
global $search,$webs,$subs;
$level = -1;
$hide = TRUE;
$webs = new Cwebs;
$subs = new Csubs;
if($page=="")$page=0;
if($search=="")$search="请输入关键字";
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_element", "stop_element");
xml_set_character_data_handler($parser, "data");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
$ret = xml_parse_from_file($parser, $file);
if(!$ret)
{
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
xml_parser_free($parser);
// 输出
echo "";
echo " ";
$i=0;
echo "";
";
$i=$page*$pagecount;
if ($webs->count > 0){
echo "".($page+1)."/";
echo Ceil($webs->count/$pagecount);}
echo "";
";
if ($webs->count > 0){
if($page>0)echo "上一页 ";
if(($page+1)count/$pagecount))echo "下一页";
}
?>
/body>
/html>
作者 来源
时间 2000年11月30日 阅读次数 247
版本 ----- 价值 17
[投他一票]
来源:奥索网
用PHP&XML编制迷你搜索引擎(四)
五、mini的搜索引擎
作了如干的铺垫,令人激动的时刻到来了。
分页等版式输出和用SQL的搜索引擎差不多,我就不加注释了。
第一段为仿sina,yahoo的按照类别查询
第二段为搜索查询部分(其实就是把整个树遍历一遍)显示符合的
内容。
说明:
"网络狂飙之谜你搜索引擎"适用于小数据量的数据索引查询,根据测试,当数据量超过1000条以后便不再适用,相当浪费主机资源,建议您超过400条记录采用mysql等数据库语言构建。希望您通过我的程序,对PHP对XML的解析函数有所认识。所有范例程序欢迎大家适用,如果您要将其用于自己的主页中请于我联系说明(其实就是将其用于的网站的名称告知于我)。
sfs(sfsz@chinese.com)
以上的所有范例请到我的主页fire.oso.com.cn
上下载,我学PHP不到一个月,也刚来oso,希望文章中的错误之处大家能谅解。以后,我将为大家奉献出更多的源创范例。
废话少说,尽请看来。
__________________________________________________________
xml2.php
html>
body>
style type=text/css>
td,p,li,input,select {font-size:12px;}
A:link {font-size:12px;color:#00007f;}
A:visited {font-size:12px;color:#00007f;}
A:active {font-size:12px;color:#ff0000;}
A:hover {font-size:12px;color:#ff0000;}
.title {font-family:Tahoma; width=420 ;font-size :16px; font-weight :bold; color :steelblue; filter:Shadow(color="LightGrey", Direction="130");}
.counter{font-family:Tahoma; color=green; font-size : 12px;}
/style>
// XML文件
$file = "demo.xml";
$pagecount = 10;
class Cweb { //网页
var $name;
var $url;
var $memo;
}
class Cwebs {
var $items = array(Cweb);
var $count = 0;
}
class Csub { //类别
var $name;
var $url;
}
class Csubs {
var $items = array(Csub);
var $count = 0;
}
function xml_parse_from_file($parser, $file)
{
if(!file_exists($file))
die("Can’t find file "$file".");
if(!($fp = @fopen($file, "r")))
die("Can’t open file "$file".");
while($data = fread($fp, 4096)) {
if(!xml_parse($parser, $data, feof($fp)))
return(false);
}
fclose($fp);
return(true);
}
function start_element($parser, $name, $attrs)
{
global $show,$level,$levelcount,$maxlevel,$hide,$lev,$num,$PHP_SELF;
global $webs,$subs;
$level += 1;
if($level>$maxlevel)$maxlevel=$level;
$levelcount[$level]+=1;
if($hide){
if($level==$lev&&$levelcount[$level]==$num)$hide=FALSE;
}else{
if($level
}
if(!$hide){
switch($name){
case "sub":
$show="sub";
break;
case "web":
$show="web";
break;
default:
break;
}
if($level==$lev+1&&$level>0){
switch($show){
case "sub":
$subs->count+=1;
$subs->items[$subs->count]->url = "$PHP_SELF?lev=$level&num=$levelcount[$level]";
break;
case "web":
$webs->count+=1;
while ( list( $key, $val ) = each( $attrs ) ) {
switch(trim($key)){
case "url" :
$webs->items[$webs->count]->url=trim($val);
break;
case "memo" :
$webs->items[$webs->count]->memo=trim($val);
break;
}
}
break;
default:
break;
}
}
}
}
function stop_element($parser, $name)
{
global $level;
$level -= 1;
}
function data($parser, $data)
{
global $level,$hide,$show,$lev,$levelcount,$num;
global $webs,$subs,$title;
if($level==$lev&&$levelcount[$level]==$num&&trim($data)!="")$title=trim($data);
if(!$hide)
if(trim($data)!=""&&($level==$lev+1&&$level>0)){
switch($show){
case "sub":
$subs->items[$subs->count]->name=trim($data);
break;
case "web":
$webs->items[$webs->count]->name=trim($data);
break;
}
}
}
//main start
global $lev,$num,$PHP_SELF;
global $title,$webs,$subs;
$level = -1;
$hide = TRUE;
$webs = new Cwebs;
$subs = new Csubs;
if($lev==""){$lev=0;$num=1;}
if($page=="")$page=0;
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_element", "stop_element");
xml_set_character_data_handler($parser, "data");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
$ret = xml_parse_from_file($parser, $file);
if(!$ret)
{
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
xml_parser_free($parser);
echo " ";
echo "
".$title."
$i=0;
echo "
"; echo "".$subs->items[$i]->name.""; echo " | ";
";
$i=$page*$pagecount;
if ($webs->count > 0){
echo "
echo Ceil($webs->count/$pagecount);}
echo "
".$i." | ". $webs->items[$i]->name." |
".$webs->items[$i]->memo; echo " |
if ($webs->count > 0){
if($page>0)echo "上一页 ";
if(($page+1)
}
?>
/body>
/html>
__________________________________________________________
xml3.php
关键字匹配采用eregi函数,功能相对简单,且有些bug,
如输入C++会报错(因为+是关键字)这点就不如用SQL查询了。
您可以在下面的程序的基础上加以完善,我这个迷你的就
起到抛砖引玉的作用吧。
html>
body>
style type=text/css>
td,p,li,input,select {font-size:12px;}
A:link {font-size:12px;color:#00007f;}
A:visited {font-size:12px;color:#00007f;}
A:active {font-size:12px;color:#ff0000;}
A:hover {font-size:12px;color:#ff0000;}
.title {font-family:Tahoma; width=420 ;font-size :16px; font-weight :bold; color :steelblue; filter:Shadow(color="LightGrey", Direction="130");}
.counter{font-family:Tahoma; color=green; font-size : 12px;}
/style>
// XML文件
$file = "demo.xml";
$pagecount = 10;
class Cweb { //网页
var $name;
var $url;
var $memo;
}
class Cwebs {
var $items = array(Cweb);
var $count = 0;
}
class Csub { //类别
var $name;
var $url;
}
class Csubs {
var $items = array(Csub);
var $count = 0;
}
// 解析XML文件的函数
function xml_parse_from_file($parser, $file)
{
if(!file_exists($file))
die("Can’t find file "$file".");
if(!($fp = @fopen($file, "r")))
die("Can’t open file "$file".");
while($data = fread($fp, 4096)) {
if(!xml_parse($parser, $data, feof($fp)))
return(false);
}
fclose($fp);
return(true);
}
function start_element($parser, $name, $attrs)
{
global $show,$level,$levelcount,$maxlevel,$PHP_SELF;
global $webs,$subs;
global $search,$finded;
$finded=FALSE;
$level += 1;
if($level>$maxlevel)$maxlevel=$level;
$levelcount[$level]+=1;
switch($name){
case "sub":
$show="sub";
break;
case "web":
$show="web";
break;
default:
break;
}
switch($show){
case "sub":
$subs->count+=1;
$subs->items[$subs->count]->url = "xml2.php?lev=$level&num=$levelcount[$level]";
break;
case "web":
$webs->count+=1;
while ( list( $key, $val ) = each( $attrs ) ) {
if(eregi($search,$val))$finded=TRUE;
switch(trim($key)){
case "url" :
$webs->items[$webs->count]->url=trim($val);
break;
case "memo" :
$webs->items[$webs->count]->memo=trim($val);
break;
}
}
break;
default:
break;
}
}
function stop_element($parser, $name)
{
global $level;
$level -= 1;
}
function data($parser, $data)
{
global $level,$show,$levelcount;
global $webs,$subs;
global $search,$finded;
if(trim($data)!=""){
switch($show){
case "sub":
$subs->items[$subs->count]->name=trim($data);
if(!eregi($search,$data))$subs->count-=1;
break;
case "web":
$webs->items[$webs->count]->name=trim($data);
if((!eregi($search,$data))&&(!$finded))$webs->count-=1;
break;
}
}
}
//main start
global $PHP_SELF;
global $search,$webs,$subs;
$level = -1;
$hide = TRUE;
$webs = new Cwebs;
$subs = new Csubs;
if($page=="")$page=0;
if($search=="")$search="请输入关键字";
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_element", "stop_element");
xml_set_character_data_handler($parser, "data");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
$ret = xml_parse_from_file($parser, $file);
if(!$ret)
{
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
xml_parser_free($parser);
// 输出
echo "";
echo "
".$search."
$i=0;
echo "
"; echo "".$subs->items[$i]->name.""; echo " | ";
";
$i=$page*$pagecount;
if ($webs->count > 0){
echo "
echo Ceil($webs->count/$pagecount);}
echo "
".$i." | ". $webs->items[$i]->name." |
".$webs->items[$i]->memo; echo " |
if ($webs->count > 0){
if($page>0)echo "上一页 ";
if(($page+1)
}
?>
/body>
/html>