Create a search engine for your website with PHP program

Source: Internet
Author: User
Tags foreach exit empty execution fread php file split trim

First, design a search form

Build a search.htm in the root directory of the site, as follows

<title> Search Forms </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body bgcolor= "#FFFFFF" text= "#000000" >
<form name= "Form1" method= "Post" action= "search.php" >
<table width= "100%" cellspacing= "0" cellpadding= "0" >
<tr>
<TD width= "36%" >
<div align= "center" >
<input type= "text" name= "keyword" >
</div>
</td>
<TD width= "64%" >
<input type= "Submit" name= "submit" value= "Search" >
</td>
</tr>
</table>
</form>
</body>

Second, the search program

Then build a search.php file in the root directory to handle the data from the Search.htm table conveys. The contents are as follows

<?php
Get search Keywords
$keyword =trim ($_post["keyword"]);
Check to see if null
if ($keyword = = "") {
echo "The keyword you are searching for cannot be empty";
exit;//End Program
}
?>

This prompts you if the keyword that the visitor entered is empty. The following is the traversal of all files.

We can iterate through all the files in a recursive way, using function Opendir,readdir or PHP directory classes. We now use the former.

<?php
Functions that traverse all files
function Listfiles ($dir) {
$handle =opendir ($dir);
while (false!== ($file =readdir ($handle))) {
if ($file!= "." && $file!= "...") {
If it's a directory, keep searching.
if (Is_dir ("$dir/$file")) {
Listfiles ("$dir/$file");
}
else{
Here to do the processing
}
}
}
}
?>

In the Scarlet Letter place we can read and process the searched files. The following is read the contents of the file, and check whether the content contains keyword $keyword, if it contains the file address assigned to an array.

<?php
$dir is the directory of the search, $keyword is the keyword of the search, $array is an array of stored
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 contents
$data =fread (fopen ("$dir/$file", "R"), FileSize ("$dir/$file"));
Do not search itself
if ($file!= "search.php") {
Whether to match
if (eregi ("$keyword", $data)) {
$array []= "$dir/$file";
}
}
}
}
}
}
Define Array $array
$array =array ();
Execution function
Listfiles (".", "PHP", $array);
Print search Results
foreach ($array as $value) {
echo "$value". <br> ";
}
?>

Now combine this result with the beginning of a program, enter a keyword, and then find that the relevant results in your site are being searched. We are now perfecting it.

1, listing the title of the content

Put

if (eregi ("$keyword", $data)) {
$array []= "$dir/$file";
}

Change into

if (eregi ("$keyword", $data)) {
if (Eregi ("<title> (. +) </title>", $data, $m)) {
$title = $m ["1"];
}
else{
$title = "no title";
}
$array []= "$dir/$file $title";
}

The principle is that if you find <title>xxx</title> in the contents of the file, then take xxx out as a title, if you can't find it, then name the title not "no title".

2, search only the topic section of the content of the Web page.

There must be a lot of HTML code in the Web page, and that's not what we want to search for, so get rid of it. I'm using regular expressions and strip_tags, and I can't get rid of all of them.

Put

$data =fread (fopen ("$dir/$file", "R"), FileSize ("$dir/$file"));
Do not search itself
if ($file!= "search.php") {
Whether to match
if (eregi ("$keyword", $data)) {

To

$data =fread (fopen ("$dir/$file", "R"), FileSize ("$dir/$file"));
if (eregi ("<body" ([^>]+) > (. +) </body>, $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". <br> ";
}

Change into

foreach ($array as $value) {
Apart
List ($filedir, $title) =split ("[]", $value, "2");
Output
echo "<a href= $filedir > $value </a>". <br> ";
}

4 Prevent timeout

If there are more files, it is necessary to prevent the PHP execution time timeout. Can be added to the file header

Set_time_limit ("600");

In seconds, so the above is limited to 10 minutes.

So the complete procedure is

<?php
Set_time_limit ("600");
Get search Keywords
$keyword =trim ($_post["keyword"]);
Check to see if null
if ($keyword = = "") {
echo "The keyword you are searching for 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 ("<body" ([^>]+) > (. +) </body>, $data, $b)) {
$body =strip_tags ($b ["2"]);
}
else{
$body =strip_tags ($data);
}
if ($file!= "search.php") {
if (eregi ("$keyword", $body)) {
if (Eregi ("<title> (. +) </title>", $data, $m)) {
$title = $m ["1"];
}
else{
$title = "no title";
}
$array []= "$dir/$file $title";
}
}
}
}
}
}
$array =array ();
Listfiles (".", "$keyword", $array);
foreach ($array as $value) {
Apart
List ($filedir, $title) =split ("[]", $value, "2");
Output
echo "<a href= $filedir target=_blank> $title </a>". <br> ";
}
?>

So far, you have done your own search engine, you can modify the Content Processing section to improve it, you can achieve the search title, or search the content of the function. You can also consider paging. Leave it all to yourself.

Here to illustrate the use of preg_match instead of Eregi, will be much faster. This is only for easy to understand, so use the commonly used eregi.



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.