Your Ad Here

WorldofPHP.NET >> Searching with PHP

Create a PHP Search Engine

Create a PHP Search Engine

Published Date : 12 Dec 2007

Author : Desi Natalia
PHP Version : PHP 4
Platform : Linux,Windows,FreeBSD,Mac OSX,Sun Solaris
 
Views : 1013
Rating : (0 votes so far)
Email to a Friend | Print this Article | Add to Favourites | Report Error

Introduction

This hands on PHP Programming article provides the knowledge necessary to design and develop a search engine for your website using PHP version 4.0 and above. Making a search engine for your website with PHP is really easy and provides substantial functionality required by most of the small to medium websites. This article introduces every steps of the development, including both design and PHP programming. Basic computer skills and knowledge of HTML fundamentals are required.
Ok, let’s begin now. 

Main

Step 1: Design Search Box

Under your website root, make a file called search.htm or anything you like and type in the following code: 

<html> <head> <title> Web Search <meta http-equiv="Content-Type" content="text/html"> </meta> <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">kr kl ds <input type="text" name="keyword"/> </div> </td> <td width="64%"> <input type="submit" name="Submit" value="Search"/> </td> </tr> </table> </form> </body> </html> }

Step 2: Write search.php file.

It is the core of your website search engine. Under your website root, create a file called search.php or anything you like.

<?php //get keywords $keyword=trim(

Introduction

This hands on PHP Programming article provides the knowledge necessary to design and develop a search engine for your website using PHP version 4.0 and above. Making a search engine for your website with PHP is really easy and provides substantial functionality required by most of the small to medium websites. This article introduces every steps of the development, including both design and PHP programming. Basic computer skills and knowledge of HTML fundamentals are required.
Ok, let’s begin now. 

Main

Step 1: Design Search Box

Under your website root, make a file called search.htm or anything you like and type in the following code: 

<html> <head> <title> Web Search <meta http-equiv="Content-Type" content="text/html"> </meta> <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">kr kl ds <input type="text" name="keyword"/> </div> </td> <td width="64%"> <input type="submit" name="Submit" value="Search"/> </td> </tr> </table> </form> </body> </html> }

Step 2: Write search.php file.

It is the core of your website search engine. Under your website root, create a file called search.php or anything you like.

<?php function listFiles($dir) { $handle=opendir($dir); while(false!==($file=readdir($handle))) { if($file!="."&amp;&amp;$file!="..") { //if it is a directory, then continue if(is_dir("$dir/$file")) { listFiles("$dir/$file"); } else { //process the searching here with the following PHP script } } } } ?>

With above, you can give hints to your users when they forget to enter a keyword. Now let’s go through all the files or articles in your website. 
<?php function listFiles($dir,$keyword,&amp;$array) { $handle=opendir($dir); while(false!==($file=readdir($handle))) { if($file!="."&amp;&amp;$file!="..") { if(is_dir("$dir/$file")) { listFiles("$dir/$file",$keyword,$array); } else { //read file $data=fread(fopen("$dir/$file","r"),filesize("$dir/$file")); //avoid search search.php itself if($file!="search.php") { //contain keyword? if(eregi("$keyword",$data)) { $array[]="$dir/$file"; } } } } } } //define array $array $array=array(); //execute function listFiles(".","php",$array); //echo/print search results foreach($array as $value) { echo "$value”" \n”; } ?>
The following scripts read, process files and check whether the files contain $keyword. If $keyword is found in the file, the file address will be saved in an array-type variable. 
if(eregi(”$keyword,$data)) { $array[]=$dir/$file”; }
Now, combine the programs listed above, you will find all the related results in your websites will be found and listed. A further optimization of the search engine can be taken by adding the following:

1 list the title of all searching results

REPLACE THE FOLLOWING:
if(eregi(”$keyword,$data)) { if(eregi(”“,$data,$m)) { $title=$m[”1?]; } else { $title=”no title”; } $array[]=$dir/$file $title”; }
WITH:
foreach($array as $value){ echo “”$value“” \n”; }
2 Add links to searching results

CHANGE THE FOLLOWING:
foreach($array as $value) { list($filedir,$title)=split(”[ ]”,$value,2?); echo$title“” \n”; }
TO:
set_time_limit(”600?);
3 Set time limit for PHP execution

ADD THE FOLLOWING AT THE BEGINNING OF PHP FILES:
<?php set_time_limit("600"); $keyword=trim(

Introduction

This hands on PHP Programming article provides the knowledge necessary to design and develop a search engine for your website using PHP version 4.0 and above. Making a search engine for your website with PHP is really easy and provides substantial functionality required by most of the small to medium websites. This article introduces every steps of the development, including both design and PHP programming. Basic computer skills and knowledge of HTML fundamentals are required.
Ok, let’s begin now. 

Main

Step 1: Design Search Box

Under your website root, make a file called search.htm or anything you like and type in the following code: 

<html> <head> <title> Web Search <meta http-equiv="Content-Type" content="text/html"> </meta> <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">kr kl ds <input type="text" name="keyword"/> </div> </td> <td width="64%"> <input type="submit" name="Submit" value="Search"/> </td> </tr> </table> </form> </body> </html> }

Step 2: Write search.php file.

It is the core of your website search engine. Under your website root, create a file called search.php or anything you like.

<?php //get keywords $keyword=trim(

Introduction

This hands on PHP Programming article provides the knowledge necessary to design and develop a search engine for your website using PHP version 4.0 and above. Making a search engine for your website with PHP is really easy and provides substantial functionality required by most of the small to medium websites. This article introduces every steps of the development, including both design and PHP programming. Basic computer skills and knowledge of HTML fundamentals are required.
Ok, let’s begin now. 

Main

Step 1: Design Search Box

Under your website root, make a file called search.htm or anything you like and type in the following code: 

<html> <head> <title> Web Search <meta http-equiv="Content-Type" content="text/html"> </meta> <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">kr kl ds <input type="text" name="keyword"/> </div> </td> <td width="64%"> <input type="submit" name="Submit" value="Search"/> </td> </tr> </table> </form> </body> </html> }

Step 2: Write search.php file.

It is the core of your website search engine. Under your website root, create a file called search.php or anything you like.

<?php function listFiles($dir) { $handle=opendir($dir); while(false!==($file=readdir($handle))) { if($file!="."&amp;&amp;$file!="..") { //if it is a directory, then continue if(is_dir("$dir/$file")) { listFiles("$dir/$file"); } else { //process the searching here with the following PHP script } } } } ?>

With above, you can give hints to your users when they forget to enter a keyword. Now let’s go through all the files or articles in your website. 
<?php function listFiles($dir,$keyword,&amp;$array) { $handle=opendir($dir); while(false!==($file=readdir($handle))) { if($file!="."&amp;&amp;$file!="..") { if(is_dir("$dir/$file")) { listFiles("$dir/$file",$keyword,$array); } else { //read file $data=fread(fopen("$dir/$file","r"),filesize("$dir/$file")); //avoid search search.php itself if($file!="search.php") { //contain keyword? if(eregi("$keyword",$data)) { $array[]="$dir/$file"; } } } } } } //define array $array $array=array(); //execute function listFiles(".","php",$array); //echo/print search results foreach($array as $value) { echo "$value”" \n”; } ?>
The following scripts read, process files and check whether the files contain $keyword. If $keyword is found in the file, the file address will be saved in an array-type variable. 
if(eregi(”$keyword,$data)) { $array[]=$dir/$file”; }
Now, combine the programs listed above, you will find all the related results in your websites will be found and listed. A further optimization of the search engine can be taken by adding the following:

1 list the title of all searching results

REPLACE THE FOLLOWING:
if(eregi(”$keyword,$data)) { if(eregi(”“,$data,$m)) { $title=$m[”1?]; } else { $title=”no title”; } $array[]=$dir/$file $title”; }
WITH:
foreach($array as $value){ echo “”$value“” \n”; }
2 Add links to searching results

CHANGE THE FOLLOWING:
foreach($array as $value) { list($filedir,$title)=split(”[ ]”,$value,2?); echo$title“” \n”; }
TO:
set_time_limit(”600?);
3 Set time limit for PHP execution

ADD THE FOLLOWING AT THE BEGINNING OF PHP FILES:
set_time_limit(”600?);
The above unit is second so ten minutes is the litmit. Now, combine all the above programs and get the complete search.php file as following:
 
<?php
   set_time_limit("600");
   $keyword=trim($_POST["keyword"]);
      if($keyword=="")
        {
            echo"Please enter your keyword";
            exit;
        }
   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)
      {
         list($filedir,$title)=split(”[ ]”,$value,”2?);
         echo ““”
         \n”;
      }
?>
Now, you have made a search engine for your website, enjoy it!

References

This article is taken from:
http://www.chauy.com/2005/11/create-a-php-search-engine

    
    
      POST
      ["keyword"]); //check if the keyword is empty if($keyword=="") { echo"no keywords"; exit; } ?>

      With above, you can give hints to your users when they forget to enter a keyword. Now let’s go through all the files or articles in your website. 
      <?php
        function listFiles($dir)
         {
           $handle=opendir($dir);
             while(false!==($file=readdir($handle)))
              {
                 if($file!="."&&$file!="..")
                 {
                   //if it is a directory, then continue
                     if(is_dir("$dir/$file"))
                     {
                        listFiles("$dir/$file");
                     }
                     else
                     {
                        //process the searching here with the following PHP script
                     }
                 }
              }
         }
      ?>
      The following scripts read, process files and check whether the files contain $keyword. If $keyword is found in the file, the file address will be saved in an array-type variable. 
      <?php
         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
                         $data=fread(fopen("$dir/$file","r"),filesize("$dir/$file"));
                       //avoid search search.php itself
                         if($file!="search.php")
                           {
                             //contain keyword?
                               if(eregi("$keyword",$data))
                                 {
                                   $array[]="$dir/$file";
                                 }
                           }
                    }
                 }
             }
          }
         //define array $array
           $array=array();
         //execute function
           listFiles(".","php",$array);
         //echo/print search results
           foreach($array as $value)
             {
               echo "$value”"
               \n”;
             }
      ?>
      Now, combine the programs listed above, you will find all the related results in your websites will be found and listed. A further optimization of the search engine can be taken by adding the following:

      1 list the title of all searching results

      REPLACE THE FOLLOWING:
      if(eregi(”$keyword”,$data))
      {
        $array[]=”$dir/$file”;
      
      }
      WITH:
      if(eregi(”$keyword”,$data))
      {
        if(eregi(”“,$data,$m))
         {
            $title=$m[”1?];
         }
        else
         {
            $title=”no title”;
         }
      
      $array[]=”$dir/$file $title”;
      }
      2 Add links to searching results

      CHANGE THE FOLLOWING:
      foreach($array as $value){
      
      echo “”$value“”
      \n”;
      
      }
      TO:
      foreach($array as $value)
      {
         list($filedir,$title)=split(”[ ]”,$value,”2?);
         echo “$title“”
         \n”;
      }
      3 Set time limit for PHP execution

      ADD THE FOLLOWING AT THE BEGINNING OF PHP FILES:
      set_time_limit(”600?);
      The above unit is second so ten minutes is the litmit. Now, combine all the above programs and get the complete search.php file as following:
       
      <?php
         set_time_limit("600");
         $keyword=trim($_POST["keyword"]);
            if($keyword=="")
              {
                  echo"Please enter your keyword";
                  exit;
              }
         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)
            {
               list($filedir,$title)=split(”[ ]”,$value,”2?);
               echo ““”
               \n”;
            }
      ?>
      Now, you have made a search engine for your website, enjoy it!

      References

      This article is taken from:
      http://www.chauy.com/2005/11/create-a-php-search-engine

        
        
          POST
          ["keyword"]); if($keyword=="") { echo"Please enter your keyword"; exit; } function listFiles($dir,$keyword,&amp;$array) { $handle=opendir($dir); while(false!==($file=readdir($handle))) { if($file!="."&amp;&amp;$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) { list($filedir,$title)=split(”[ ]”,$value,”2?); echo ““” \n”; } ?>
          The above unit is second so ten minutes is the litmit. Now, combine all the above programs and get the complete search.php file as following:
           
          <?php
             set_time_limit("600");
             $keyword=trim($_POST["keyword"]);
                if($keyword=="")
                  {
                      echo"Please enter your keyword";
                      exit;
                  }
             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)
                {
                   list($filedir,$title)=split(”[ ]”,$value,”2?);
                   echo ““”
                   \n”;
                }
          ?>
          Now, you have made a search engine for your website, enjoy it!

          References

          This article is taken from:
          http://www.chauy.com/2005/11/create-a-php-search-engine

            
            
              POST
              ["keyword"]); //check if the keyword is empty if($keyword=="") { echo"no keywords"; exit; } ?>

              With above, you can give hints to your users when they forget to enter a keyword. Now let’s go through all the files or articles in your website. 
              <?php
                function listFiles($dir)
                 {
                   $handle=opendir($dir);
                     while(false!==($file=readdir($handle)))
                      {
                         if($file!="."&&$file!="..")
                         {
                           //if it is a directory, then continue
                             if(is_dir("$dir/$file"))
                             {
                                listFiles("$dir/$file");
                             }
                             else
                             {
                                //process the searching here with the following PHP script
                             }
                         }
                      }
                 }
              ?>
              The following scripts read, process files and check whether the files contain $keyword. If $keyword is found in the file, the file address will be saved in an array-type variable. 
              <?php
                 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
                                 $data=fread(fopen("$dir/$file","r"),filesize("$dir/$file"));
                               //avoid search search.php itself
                                 if($file!="search.php")
                                   {
                                     //contain keyword?
                                       if(eregi("$keyword",$data))
                                         {
                                           $array[]="$dir/$file";
                                         }
                                   }
                            }
                         }
                     }
                  }
                 //define array $array
                   $array=array();
                 //execute function
                   listFiles(".","php",$array);
                 //echo/print search results
                   foreach($array as $value)
                     {
                       echo "$value”"
                       \n”;
                     }
              ?>
              Now, combine the programs listed above, you will find all the related results in your websites will be found and listed. A further optimization of the search engine can be taken by adding the following:

              1 list the title of all searching results

              REPLACE THE FOLLOWING:
              if(eregi(”$keyword”,$data))
              {
                $array[]=”$dir/$file”;
              
              }
              WITH:
              if(eregi(”$keyword”,$data))
              {
                if(eregi(”“,$data,$m))
                 {
                    $title=$m[”1?];
                 }
                else
                 {
                    $title=”no title”;
                 }
              
              $array[]=”$dir/$file $title”;
              }
              2 Add links to searching results

              CHANGE THE FOLLOWING:
              foreach($array as $value){
              
              echo “”$value“”
              \n”;
              
              }
              TO:
              foreach($array as $value)
              {
                 list($filedir,$title)=split(”[ ]”,$value,”2?);
                 echo “$title“”
                 \n”;
              }
              3 Set time limit for PHP execution

              ADD THE FOLLOWING AT THE BEGINNING OF PHP FILES:
              set_time_limit(”600?);
              The above unit is second so ten minutes is the litmit. Now, combine all the above programs and get the complete search.php file as following:
               
              <?php
                 set_time_limit("600");
                 $keyword=trim($_POST["keyword"]);
                    if($keyword=="")
                      {
                          echo"Please enter your keyword";
                          exit;
                      }
                 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)
                    {
                       list($filedir,$title)=split(”[ ]”,$value,”2?);
                       echo ““”
                       \n”;
                    }
              ?>
              Now, you have made a search engine for your website, enjoy it!

              References

              This article is taken from:
              http://www.chauy.com/2005/11/create-a-php-search-engine

                
                


                  Other Related and Popular Articles :

                  Making a PHP Login Form
                  This article shows on how to make a PHP login form
                  PHP Form Mailer
                  How to create PHP Form Mailer to stop spammer
                  Developing a Login System with PHP and MySQL
                  Developing a Login System with PHP and MySQL
                  Web Services Implementation using PHP and SOAP
                  Basic Introduction on PHP , Web Services and SOAP
                  PHP security tips for upload files
                  Always restrict the file types that you allow and don’t rely on a blacklist approach.

                  Author Profile : Desi Natalia

                  Click here to view Author Profile


                  How would you rate the quality of this content?
                  Poor Excellent

                  Comments

                  Leave New Comments


                  Article Content copyright by Desi Natalia
                  Everything else Copyright © by WorldofPHP.NET 2008