Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > PHP
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 10-25-2005, 06:20 AM   #1 (permalink)
zhisede
Registered User
 
Join Date: Oct 2005
Posts: 1
zhisede is on a distinguished road
Make a search engine for your website with PHP

I found this article from another forum, enjoy, , pls tell me the result if sb tried it

----------------------------------------------------------------------------------------------------

Make a search engine for your website with PHP

by Rory Canyon

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.

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:
PHP Code:
<html>
 <
head>
  <
title>Web Search</title>
  <
meta http-equiv="Content-Type" content="text/html; charset=gb2312">
 </
head>
 <
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>
</
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 Code:
<?php
//get keywords
$keyword=trim($_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 Code:
<?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 Code:
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
PHP Code:
if(eregi("$keyword",$data)){
  
$array[]="$dir/$file";

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

2,Add links to searching results

CHANGE THE FOLLOWING
PHP Code:
foreach($array as $value){
  echo 
"$value"."\n";

TO
PHP Code:
foreach($array as $value){ 
  list(
$filedir,$title)=split("[ ]",$value,"2"); 
  echo 
"$value"."\n";

3 Set time limit for PHP execution

ADD THE FOLLOWING AT THE BEGINNING OF PHP FILES

PHP Code:
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 Code:
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("<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){
  list(
$filedir,$title)=split("[ ]",$value,"2"); 
  echo 
"$title "."\n";
?> 
Now, you have made a search engine for your website, enjoy it!


.
__________________

Last edited by redhead : 10-25-2005 at 07:48 AM. Reason: Fixing php-code tags and indentation along the entire article
zhisede is offline   Reply With Quote
Old 10-25-2005, 07:39 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
woohoo! 1st post link spammer
__________________
sde is offline   Reply With Quote
Old 10-25-2005, 07:51 AM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
redhead is on a distinguished road
Yeah, but I fxed it, and made it look like a usefull article..
Perhaps I should convert it into a PHP-howto, and close this thread...
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-25-2005, 07:54 AM   #4 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 595
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
I guess Tuesday is Spam day?
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Designing a search engine simpler than GOOGLE apponez Program Design and Methods 4 03-06-2006 08:02 PM
PHP 5.0.4 and 4.3.11 Released sde Code Newbie News 0 04-20-2005 10:56 AM
Search Engine Submittions Eyelfixit Lounge 12 07-25-2003 08:09 PM
Please Review ZapMeta - New Search Engine khn Program Design and Methods 3 07-06-2003 09:15 AM


All times are GMT -8. The time now is 06:50 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle