Use PHP programs to create a search engine for your website

Source: Internet
Author: User
1. create a search.htm in the root directory of the website with the following Content: htmlheadtitle search form/titlemetahttp-equiv = "content-Type" Content = "text/html; charset = gb2312"/headbodybgcolor = "# fffftex"

1. design search forms

Create a search.htm file under the root directory of the website. The content is as follows:



Search form





2. search programs

Create a search. php file under the root directory to process the data transmitted from the search.htm form. the content is as follows:

// Obtain key words for search
$ Keyword = trim ($ _ POST ["keyword"]);
// Check whether it is empty
If ($ keyword = ""){
Echo "the keyword you want to search for cannot be blank ";
Exit; // end the program
}
?>

In this way, if the keyword entered by the visitor is empty, a prompt can be prompted. The following shows how to traverse all objects.

We can use the progressive method to traverse all files. we can use the opendir, readdir, and PHP Directory functions. We now use the former.

// Function for traversing all objects
Function listFiles ($ dir ){
$ Handle = opendir ($ dir );
While (false! ==( $ File = readdir ($ handle ))){
If ($ file! = '.' & $ File! = '..'){
// Continuous search for directories
If (is_dir ('$ dir/$ file ')){
ListFiles ('$ dir/$ file ');
}
Else {
// Process it here
}
}
}
}
?>

In the red text, we can read and process the searched files. the following describes how to read the file content and check whether the content contains the keyword $ keyword. if the content contains the keyword $ keyword, assign the file address to an array.

// $ Dir is the search directory, $ keyword is the key word for search, and $ array is the storage 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 the file content
$ Data = fread (fopen ('$ dir/$ file', 'r'), filesize (' $ dir/$ file '));
// Do not search for itself
If ($ file! = "Search. php "){
// Match
If (eregi ('$ keyword', $ data )){
$ Array [] = '$ dir/$ file ';
}
}
}
}
}
}
// Define an array $ array
$ Array = array ();
// Perform the function
ListFiles ('.', 'php', $ array );
// Print search results
Foreach ($ array as $ value ){
Echo '$ value '.'
';
}
?>

Now we can combine this result with a program at the beginning and enter a keyword. then we will find all the relevant results on your website. We are now improving it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.