?????????
??????? <\/div>
????? <\/td>
?????
??????? ????? <\/td> ??? <\/tr> ? <\/table> <\/form> <\/body> <\/html><\/p>\n 二,搜索程序 再在根目錄下建個(gè)search.php 的文件,用來處理search.htm表單傳過來的數(shù)據(jù).內(nèi)容如下 \/\/獲取搜索關(guān)鍵字 $keyword=trim($_POST[“keyword”]); \/\/檢查是否為空 if($keyword==””){ echo”您要搜索的關(guān)鍵字不能為空”; exit;\/\/結(jié)束程序 } ?><\/p>\n
這樣如果訪問者輸入的關(guān)鍵字為空時(shí),可以做出提示。下面是遍歷所有文件。<\/p>\n 我們可以用遞歸的方法遍歷所有的文件,可以用函數(shù)opendir,readdir,也可以用PHP Directory的類。我們現(xiàn)在用前者. \/\/遍歷所有文件的函數(shù) function listFiles($dir){ $handle=opendir($dir); while(false!==($file=readdir($handle))){ if($file!=\".\"&&$file!=\"..\"){ \/\/如果是目錄就繼續(xù)搜索 if(is_dir(\"$dir\/$file\")){ listFiles(\"$dir\/$file\"); } else{ \/\/在這里進(jìn)行處理 } } } }<\/P> ?><\/p>\n 在紅字的地方我們可以對搜索到的文件進(jìn)行讀取,處理.下面就是讀取文件內(nèi)容,并檢查內(nèi)容中是否含有關(guān)鍵字$keyword,如果含有就把文件地址賦給一個(gè)數(shù)組。 \/\/$dir是搜索的目錄,$keyword是搜索的關(guān)鍵字 ,$array是存放的數(shù)組 function listFiles($dir,$keyword,&$array){ $handle=opendir($dir); while(false!==($file=readdir($handle))){ if($file!=\".\"&&$file!=\"..\"){ if(is_dir(\"$dir\/$file\")){ listFiles(\"$dir\/$file\",$keyword,$array); } else{ \/\/讀取文件內(nèi)容 $data=fread(fopen(\"$dir\/$file\",\"r\"),filesize(\"$dir\/$file\")); \/\/不搜索自身 if($file!=”search.php”){ \/\/是否匹配 if(eregi(\"$keyword\",$data)){ $array[]=\"$dir\/$file\"; } } } } } } \/\/定義數(shù)組$array $array=array(); \/\/執(zhí)行函數(shù) listFiles(\".\",\"php\",$array); \/\/打印搜索結(jié)果 foreach($array as $value){ echo \"$value\".\" \\n\"; } ?><\/p>\n 現(xiàn)在把這個(gè)結(jié)果和開頭的一段程序結(jié)合起來,輸入一個(gè)關(guān)鍵字,然后就會(huì)發(fā)現(xiàn)你的網(wǎng)站中的相關(guān)結(jié)果都被搜索出來了。我們現(xiàn)在在把它完善一下。 1,列出內(nèi)容的標(biāo)題 把 ????????????????????????? if(eregi(\"$keyword\",$data)){ ????????????????? $array[]=\"$dir\/$file\"; ????????????????????????? } 改成 ????????????????????????? if(eregi(\"$keyword\",$data)){ ?????????????????????????????????? if(eregi(\" (.+)<\/title>\",$data,$m)){ ??????????????????????? $title=$m[\"1\"]; ?????????????????????????????????? } ?????????????????????????????????? else{ ??????????????????????? $title=\"沒有標(biāo)題\"; ?????????????????????????????????? } ?????????????????????????????????? $array[]=\"$dir\/$file $title\"; ?????????????????????????? } 原理就是,如果在文件內(nèi)容中找到xxx<\/title>,那么就把xxx取出來作為標(biāo)題,如果找不到那么就把標(biāo)題命名未”沒有標(biāo)題”.<\/p>\n2,只搜索網(wǎng)頁的內(nèi)容的主題部分。 做網(wǎng)頁時(shí)一定會(huì)有很多html代碼在里面,而這些都不是我們想要搜索的,所以要去除它們。我現(xiàn)在用正則表達(dá)式和strip_tags的配合,并不能把所有的都去掉。 把 ??????????? $data=fread(fopen(\"$dir\/$file\",\"r\"),filesize(\"$dir\/$file\")); ??????????? \/\/不搜索自身 ??????????? if($file!=”search.php”){ ????????????? \/\/是否匹配 ????????????????????????? if(eregi(\"$keyword\",$data)){ 改為 $data=fread(fopen(\"$dir\/$file\",\"r\"),filesize(\"$dir\/$file\")); ?????????? if(eregi(\"
]+)>(.+)<\/body>\",$data,$b)){ ???????????????? $body=strip_tags($b[\"2\"]); ??????????????????????? } ??????????????????????? else{ ???????????????? $body=strip_tags($data); ??????????????????????? } ??????????????????????? if($file!=\"search.php\"){ ??????????????????????????? if(eregi(\"$keyword\",$body)){<\/p>\n3,標(biāo)題上加鏈接 foreach($array as $value){ ?? echo \"$value\".\" \\n\"; } 改成 foreach($array as $value){ ?? \/\/拆開 ?? list($filedir,$title)=split(“[ ]”,$value,”2”); ?? \/\/輸出 ?? echo \"$value<\/a>\".\" \\n\"; } 4防止超時(shí) 如果文件比較多,那么防止PHP執(zhí)行時(shí)間超時(shí)是必要的??梢栽谖募^加上 set_time_limit(“600”); 以秒為單位,所以上面是設(shè)10分鐘為限。<\/p>\n So the complete program is set_time_limit(\"600\"); \/\/Get the search keyword $keyword=trim($_POST[\"keyword \"]); \/\/Check if it is empty if($keyword==\"\"){ echo \"The keyword you want to search cannot be empty\"; exit;\/\/End Program } function listFiles($dir,$keyword,&$array){ $handle=opendir($dir); while(false!==($file=readdir($ handle))){ If($file!=\".\"&&$file!=\"..\"){ if(is_dir(\"$dir\/$file\")){ listFiles( \"$dir\/$file\",$keyword,$array); ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????. $dir\/$file\")); If(eregi(\" ]+)>(.+)<\/body>\",$data,$b)){ ??????????????? $body=strip_tags($b[\"2\"]);???????????????????????????????????????????????????$body=strip_tags($data); ????????????????????????????????????????????????????????????????????????????????????\"search.php\"){ if(eregi(\"$keyword\",$body)){ if(eregi(\"(.+)<\/title&g t;\",$data, $m)){ ???????????????????????????????????????????????????????????????????????????????????????????else{ $title=\"No title\"; } $array[] =\"$dir\/$file $title\"; ??????????????????????????????????????????????????????????????????????????????????????????????????????????>} $array=array(); listFiles( \".\",\"$keyword\",$array); foreach($array as $value){ \/\/Split list($filedir,$title)=split(\"[ ]\" ,$value,\"2\"); \/\/Output echo \"$title <\/a>\".\" n\"; } ?>
So far, you have built your own search engine. You can also improve it by modifying the content processing part. You can search for titles or search for content. Function. Also consider pagination. Keep this to yourself.
Here is a description of using preg_match instead of eregi, which will be much faster. This is just for easy understanding, so the commonly used eregi is used.
\n <\/p>\n<\/p>\n \nhttp:\/\/www.bkjia.com\/PHPjc\/314558.html<\/span>www.bkjia.com<\/span>true<\/span>http: \/\/www.bkjia.com\/PHPjc\/314558.html<\/span>TechArticle<\/span>ccterran (original work) Author: iwind A friend made a website using Dreamweaver. There is no dynamic content, just some personal collections. articles, personal introductions, etc. There is more content now...<\/span>\n<\/div>\n |