#native_company# #native_desc#
#native_cta#

Simple text searching

By Gemma
on October 27, 2005

Version: 2.0

Type: Sample Code (HOWTO)

Category: File Management

License: GNU General Public License

Description: Simple script for searching all the folders and files(html & php) in your site and displaying the search result found and the visitor to the result page

<?php
$dir = array('dental/', './');
// as many dirs as you like. Main dir is the ./ one
// Your Search Key word comes here 
//$searchstr="Keyword";
//this is to get it from a GET form
$searchstr = $_GET['word'];
// number of words to display as result
$n = 10;
// your domain
$dominio = "http://www.thedomainyouhavethison.com/";
$searchstr = htmlentities($searchstr);
echo "<p>Search by <strong>"$searchstr"</strong></p>";
echo "<ol>";
// Open a known directory, and proceed to read its contents .You can insert many number of directories in the array 
for($i=0;$i<2;$i++) //Instead of 2 you can insert the  number of folders to be searched
{
	if (is_dir($dir[$i])) {
		if ($dh = opendir($dir[$i])) {
		$findstrcontent=array();$file=array();$fileContent=array();
		$cnt=0;$count=0;
			while (($file[$cnt]= readdir($dh)) !== false) { 
			//echo $dir[$i].$file[$cnt]."<br />";
				//echo "filename: $file[$cnt]<br />";
			
			$mirohtm = substr($file[$cnt], -4, 4); 
			$mirohtml = substr($file[$cnt], -5, 5);  
			//limited to .htm and .html files
				if (($mirohtm == ".htm" ) || ($mirohtml == ".html" )) {
				//echo $dir[$i].$file[$cnt]."<br />";
				//echo "filename: $file[$cnt]<br />";
				$fileContent[$cnt]=file_get_contents($dir[$i].$file[$cnt]);
				//echo strip_tags($fileContent[$cnt]);
				$limpio = strip_tags($fileContent[$cnt]);
					if($dom=@stristr($limpio,$searchstr)){
						//$findstrcontent[$cnt]=strip_tags(substr($dom,0,100)); 
						$findstrcontent[$cnt] = implode(" ", array_slice(explode(" ", $dom), 0, $n));
 						$link=$dir[$i].$file[$cnt];
						$findstrcontent[$cnt] = str_replace($searchstr,"<strong>".$searchstr."</strong>", $findstrcontent[$cnt]);
							if($findstrcontent[$cnt]!=""){
								$count++;
								echo "<li>";
								$path = $dir[$i].$file[$cnt];
								$path = str_replace ("./","", $path);
								?>
								
								<? echo $findstrcontent[$cnt]; ?>... 
								<br />
								
								<a href=<?=$link?>><? echo "<em> ".$dominio.$path."</em>"; ?></a><br />
								<?
								echo "</li>"; 
							 } 
					}
				}  
			$cnt=$cnt+1;
			}
		
		closedir($dh);
		}
	}
}
echo "</ol>";
if ($count > 0) {
echo "Found <strong>$count</strong> results<br />";
} else {
echo "Sorry, no results.<br />";
}
echo "<br />Thanks for searching<br />";
?>