Home  >  Article  >  Backend Development  >  Full text search code in php site_PHP tutorial

Full text search code in php site_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:22:57822browse

php站内全文搜索代码

 如果你是用dreamweaver或者frontpage设计的,那么除非你故意删掉,它们都在存在的。下面就让我们一步步来完成并在工程中改善这个搜索引擎。

一,设计搜索表单
在网站的根目录下建个search.htm,内容如下


搜索表单




 



 









2. Search program
Then create a search.php file in the root directory to process the data passed by the search.htm form. The content is as follows
//Get search keywords
$keyword=trim($_POST[“keyword”]);
//Check if it is empty
if($keyword==””){
echo "The keyword you want to search cannot be empty";
exit;//End program
}
?>

This way, if the keyword entered by the visitor is empty, a prompt can be made. Below is a loop through all files.

We can traverse all files recursively, using the functions opendir, readdir, or the PHP Directory class. We use the former now.
//Function to traverse all files
function listFiles($dir){
$handle=opendir($dir);
while(false!==($file=readdir($handle))){
if($file!="."&&$file!=".."){
//If it is a directory, continue searching
if(is_dir("$dir/$file")){
listFiles("$dir/$file");
}
else{
//Process here
}
}
}
}

?>

In the red text, we can read and process the searched files. The following is to read the file content and check whether the content contains the keyword $keyword. If it does, assign the file address to an array.
//$dir is the search directory, $keyword is the search keyword, $array is the stored array
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{
//Read file content
$data=fread(fopen("$dir/$file","r"),filesize("$dir/$file"));
//Do not search for itself
if($file!=”search.php”){
//Whether it matches
if(eregi("$keyword",$data)){
$array[]="$dir/$file";
}
}
}
}
}
}
//Define array $array
$array=array();
//Execute function
listFiles(".","php",$array);
//Print search results
foreach($array as $value){
echo "$value"."
n";
}
?>

Now combine this result with the program at the beginning, enter a keyword, and then you will find that the relevant results in your website have been searched. We are improving it now.
1. List the title of the content
Put
if(eregi("$keyword",$data)){
$array[]="$dir/$file";
}
Change to
if(eregi("$keyword",$data)){
if(eregi("(.+)",$data,$m)){
$title=$m["1"];
}
else{
$title="No title";
}
$array[]="$dir/$file $title";
}
The principle is that if xxx is found in the file content, then take out xxx as the title. If it cannot be found, then name the title as "no title".

2. Only search the topic part of the content of the web page.
When making a web page, there must be a lot of html code in it, and these are not what we want to search for, so we need to remove them. I am now using regular expressions with strip_tags, but not all of them can be removed.
Put
$data=fread(fopen("$dir/$file","r"),filesize("$dir/$file"));
//Do not search for itself
if($file!=”search.php”){
//Whether it matches
if(eregi("$keyword",$data)){
Change to
$data=fread(fopen("$dir/$file","r"),filesize("$dir/$file"));
if(eregi("]+)>(.+)",$data,$b)){
$body=strip_tags($b["2"]);
}
else{
$body=strip_tags($data);
}
if($file!="search.php"){
if(eregi("$keyword",$body)){

3. Add a link to the title
foreach($array as $value){
echo "$value"."
n";
}
Change to
foreach($array as $value){
//Disassemble
list($filedir,$title)=split(“[ ]”,$value,”2”);
//Output
echo "$value"."
n";
}
4 Prevent timeouts
If there are many files, it is necessary to prevent PHP execution time from timing out. You can add
to the file header set_time_limit(“600”);
The unit is seconds, so the limit above is 10 minutes.


So the complete program is
set_time_limit("600");
//Get search keywords
$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);
}
else{
$data=fread(fopen("$dir/$file","r"),filesize("$dir/$file"));
if(eregi("]+)>(.+)",$data,$b)){
$body=strip_tags($b["2"]);
}
else{
$body=strip_tags($data);
}
if($file!="search.php"){
if(eregi("$keyword",$body)){
if(eregi("(.+)",$data,$m)){
$title=$m["1"];
}
else{
$title="No title";
}
$array[]="$dir/$file $title";
}
}
}
}
}
}
$array=array();
listFiles(".","$keyword",$array);
foreach($array as $value){
//Disassemble
list($filedir,$title)=split("[ ]",$value,"2");
//Output
echo "$title "."
n";
}
?>

So far, you have built your own search engine. You can also improve it by modifying the content processing part, and you can implement the function of searching titles or searching content. 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/845141.htmlTechArticlephp full-text search code in the site. If you designed it with dreamweaver or frontpage, then unless you delete it deliberately, they will in existence. Let's complete it step by step and start the project...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn