WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : SiteSearch 1.1: This app lets end users search your site for keywords. You specify which directories should be included in the search.
Categories : Search, Search Engines, PHP Click here to Update Your Picture
Sheridan Saint-Michel
Date : Feb 24th 2001
Grade : 3 of 5 (graded 10 times)
Viewed : 9023
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Sheridan Saint-Michel
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?php

//Sitesearch v1.1 - Sheridan Saint-Michel

//Begin Environment Variables. You need to set these!//

//List of directories to be included in the search.
//Usage "path" => "url" (path to directory and URL which corresponds to directory)
//remember to include the trailing / on the URL

$directories = array(
"/home/mysite/HTML" => "http://www.mysite.com/",
"/home/mysite/HTML/sales" => "http://www.mysite.com/sales/",
"/home/mysite/HTML/products" => "http://www.mysite.com/products/"
);

//End Environment Variables//

Function Keyword_Check($filenames,$keywords)
{

for($i = 0; $i < count($filenames); $i++)
{
$filename = $filenames[$i];
$match = 0;
$fd = fopen($filename, "r");
$contents = fread($fd, filesize ($filename));
fclose($fd);
//Remove HTML Tags before searching
$search = array ("'<script[^>]*?>.*?</script>'si", // Strip out javascript
"'<[\/\!]*?[^<>]*?>'si", // Strip out html tags
"'([\r\n])[\s]+'", // Strip out white space
"'&(quot|#34);'i", // Replace html entities
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&#(\d+);'e"); // evaluate as php

$replace = array ("",
" ",
"\\1",
"\"",
"&",
"<",
">",
" ",
chr(161),
chr(162),
chr(163),
chr(169),
"chr(\\1)");

$contents = preg_replace ($search, $replace, $contents);
$contents = preg_replace ("/\W/", " ", $contents);
$contents = preg_replace ("/\s+/", " ", $contents);

//Seperate Each Word into an Array Element and Compare to Keywords
$contents = explode(" ", $contents);
$j = 0;
for($j = 0; $j < count($keywords); $j++)
{
for($k = 0; $k < count($contents); $k++)
{
//compare contents with each keyword
if (!strcasecmp ($contents[$k], $keywords[$j]))
{
$match++;
break;
}
}
}

if ($match == count($keywords) )
$retVal[count($retVal)] = $filename;
}
return $retVal;
}

function Get_Filenames($directory)
{
//Load Directory Into Array
$handle=opendir($directory);
while ($file = readdir($handle))
{
if ($file != "." && $file != "..")
$retVal[count($retVal)] = $file;
}

//Clean up and sort
closedir($handle);
sort($retVal);
return $retVal;
}

if ( isset($keyword) )
{
$keywords = explode(" ", $keyword);
$pages = array();
while (list ($key, $val) = each ($directories))
{
$directory = $key;
chdir($directory) or die("Directory $directory Not found");
$filenames = Get_Filenames($directory);
$found = Keyword_Check($filenames,$keywords);

//add any pages with keywords in current directory to array
for($i = 0;$i < count($found); $i++)
{
$add = "$val$found[$i]";
$pages[count($pages)] = $add;
}
}

?>

<HTML>
<Head>
<Title>Website Search</Title>
</Head>
<?php } ?>
<Body>
<Form Action=search.php>
<Input Type=Text Name=keyword Value="$keyword";>
<Input Type=Submit Value=Search>
<Input Type=Reset Value="New Search">
<?php
if ( isset($keyword) )
{
echo "<HR>\n";
echo "<Font Color=Blue>$numfound pages matching your query were found</Font>";
echo "<HR>";
for ($i = 0; $i < count($pages); $i++)
echo "<A HREF=\"$pages[$i]\">$pages[$i]</A><BR>";
}
?>
</Form>
</Body>
</HTML>



How to build a search query for any N number of words in a search string
Categories : PHP, Regexps, Search Engines, Search
Searches through a local INN server's discussions
Categories : Search, Complete Programs, PHP
PageRank Display
Categories : Search Engines, HTML and PHP, PHP
http://phpMySearch.web4.hm - The phpMySearch search engine system is a completeworld wide web indexing and searching system for a small domain or intranet.
Categories : Search Engines, PHP, Databases, MySQL
Boolean Keyword Interpreter
Categories : PHP, Algorithms, Search Engines
UDMSearch - a free search engine, indexing system.
Categories : Search Engines, Linux, PHP, MySQL, ODBC
free, search engine, indexing, system, information, web, ftp, http, free, software, cgi, php, MySQL, database, php3, FreeBSD, Linux, Unix, UdmSearch
Categories : MySQL, Complete Programs, PHP, Databases, Search
SubmitForce URL power submitter (searchengine submission class)
Categories : PHP, Search Engines, URLs, PHP Classes
Add Boolean Logic Functions to Database Queries Easily. A function to write a WHERE clause dynamically to search a database from a search form.
Categories : MySQL, PHP, Search
Extended Get File List Function
Categories : PHP, Filesystem, Search, Directories
Client classes for Dictionary servers UPDATED: 2000-06-06
Categories : Network, Search, Complete Programs, PHP Classes, PHP
whois domain name lookup dns url
Categories : PHP, Search, Network
Search for files
Categories : PHP, Filesystem, Search
Dynamic pages with no "?"
Categories : PHP, Search Engines
Search and Replace Text : Searches Files for Specified Text and Replaces It by a Given Text
Categories : PHP, PHP Classes, Search, Filesystem
 Dave Carrera wrote : 591
I like the simplisity of this script BUT... is the value="$keyword" in the form area a typo as it displays $keyword in the search box on each reload...should it be name="$keyword" ???? good and cool script
 
 Sheridan Saint-Michel wrote : 592
You`re right that was an oversight  =)  Change
&lt;Input Type=Text Name=keyword Value="$keyword";&gt;

to read

&lt;?php echo "&lt;Input Type=Text Name=keyword Value=\"$keyword\"&gt;"; ?&gt;

and it should work right  =)
 
 Bill Williams wrote : 640
Excellent script!  But there are some more minor problems:

1) If you search for a "space", then it lists all files and sub-directories of the root!  Right after Get_Filenames(), Change this line:

if ( isset($keyword)) { 

to:

if ( isset($keyword) && strlen($keyword) &gt; 0) { 


2) You never use the variable $numfound!  I set it to 0 at the top of the script, then added this after the form:

        if ($numfound &gt; 0) {
            echo "&lt;font color=\"#0000cc\"&gt;$numfound Page(s) matching your query were found:&lt;/font&gt;"; 
            echo "&lt;hr noshade size=\"1\" color=\"#dddddd\"&gt;\n";  
        } else {
            echo "&lt;font color=\"#cc0000\"&gt;No Pages matching your query were found.  Please search again.&lt;/font&gt;"; 
        }

I also added:

$numfound += 1; 

in the loop after the comment "//add any pages with keywords in current directory to array"

3) And finally, you include sub-directores in your results, a HUGE security hole, since I don`t want visitors to know where my admin or includes directories reside!  Change this:

if ($file != "." && $file != ".." && !is_dir($file)) { $retVal[count($retVal)] = $file; }

in the conditional in Get_Filenames()
 
 Sheridan Saint-Michel wrote : 642
I just looked over it again and discovered that a line somehow got missed when I copied it to the site.  In the script I am running I have the line

$numfound = count($pages);

Here

if ( isset($keyword) )  
{  
  $keywords = explode(" ", $keyword);  
  $pages = array();  
  while (list ($key, $val) = each ($directories))   
  {  
    $directory = $key;  
    chdir($directory) or die("Directory $directory Not found");  
    $filenames = Get_Filenames($directory);  
    $found = Keyword_Check($filenames,$keywords);  
    
    //add any pages with keywords in current directory to array  
    for($i = 0;$i &lt; count($found); $i++)  
    {  
      $add = "$val$found[$i]";  
      $pages[count($pages)] = $add;  
    }  
  }  

$numfound = count($pages);
?&gt;  

Not sure how I managed to lose a line of code.... Copy and Paste has never failed me before  =)

Also thanks for the comment concerning the space  =)
 
 Sheridan Saint-Michel wrote : 643
I also added the line you suggested concerning the subdirectories.  Thanks a lot, I appreciate it  =)
 
 mark Pau wrote : 657
Very nice program......  But something want to know!
How to change the search result only ".html" and ".htm" files ? 
 
 Sheridan Saint-Michel wrote : 659
Any changes to the types of files returned would be accomplished by changing the line

    if ($file != "." && $file != "..")   

in the Get_Filenames Function.

So to only return .htm and .html you would change that line to read something like

if(substr($text[$j], -4, 4) == ".htm" || substr($text[$j], -5, 5) == ".html")

Hope that helps
Sheridan
 
 Heinz Ernst wrote : 689
How do I manage to have the script working on a windows-system? I have Apache installed as standalone on a NT4-Machine. I appreciate helpful hints. Thanks a lot.
Heinz
 
 john culby wrote : 713
i want to search for a string such as  word-word-word  so i guess my question is how do i strip out a dash
thanks
 
 Christopher Leon wrote : 717
I am running this on windows2000 server, and I can get it to run if I set it to look in one directory, but when I open it up to a directory that has many subdirs, I get this message:

Warning: fopen("10.16.01","r") - Permission denied in L:\lvweb\search.php on line 24

it will also say it for the directory that I was originally searching in, and I know that it has read rights

thanks
 
 kenneth saandvig wrote : 779
I am having problems with this script... When uploading it and going to the page, it produces this output:

Parse error: parse error in /home/httpd/mywebsite/www/search.php on line 7

What a`m I doing wrong????
 
 kenneth saandvig wrote : 780
I was a little quick before... I just had to copy the "plain text". sorry =)
This is a VERY nice script 


Thanx=) 
 
 java maniacs wrote : 787
hmm... I get this:

Warning: fopen(".search.phtml.swp", "r") - Permission denied in /home/javamaniacs/www/dev/search.phtml on line 24

Warning: Supplied argument is not a valid File-Handle resource in /home/javamaniacs/www/dev/search.phtml on line 25

Warning: Supplied argument is not a valid File-Handle resource in /home/javamaniacs/www/dev/search.phtml on line 26
 
 java maniacs wrote : 788
D`oh! Never mind. Had the file open in vi and was trying to search a swap file! ....
 
 Sheridan Saint-Michel wrote : 789
Version 1.2 lets you filter out files from the search, so you shouldn`t run into this.  I`ll post a copy here in the next few days, or you can view it at my homepage:
http://www.onlychildclub.com/Lysander/Code/
 
 Jackson Lim wrote : 799
err... can anyone explain to me how to use the codes?
I would really appreciate it. 
Thank you
(just started learning, a bit confused) 
 
 gili sabach wrote :1024
Hello!
Great script, but I found that he does`nt support active search in my own language.
for example: if I`ll type word search in hebrew language he won`t find nothing, can I do something to fix this?
thank`s and again nice and really helping script.
soory about my english and have a nice day!
waiting for answer, Gili.