Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
Search keyword with PHP (0 Comments)
Admin: Posted Date: April 4, 2010

This tutorial will teach you how you can use PHP to search keywords on your website. This is very easy to use and so helpful to your visitors.

Search Keyword in all pages of a website :

This is simplest way, probably to search for a keyword in a page. Please let us know if you are looking for something different.

<?php
$file_name=array();
$search_string=$_GET["searchstring"];
$search_result="";
$files=""; $data="";
$link="";
$phpfilename="";
$textstart=0; $textend=0; $strlocation=0;
$numbers=array(5,10,6,8,15,20);
$i=0;	
if($search_string!="")
{//ends at last
if ($handle = opendir('name_of_dir')) 
{ 
// List all the files 
while (false !== ($file = readdir($handle)))
{
$filename[]= $file; 
} 
closedir($handle); 
} 
//read the file get the text and filter the search...

  foreach($filename as $value)
{
$files="pages/$value";
$fp=fopen($files,"r") or die("unable to read a file");
$data="";
while(!feof($fp))
{
$data.=strip_tags(fgets($fp));
}
if(strpos($data,$search_string)!=false)
{
$phpfilename=substr($value,0,strpos($value,"."));
$link=$phpfilename.".php";
if($link=="home.php") $link="index.php";
$strlocation=strpos($data,$search_string);
$i=array_rand($numbers);
if(strpos($data,".",$strlocation)!=false)
$textend=strpos($data,".",$strlocation);
elseif(strpos($data," ",$strlocation)!=false)
$textend=strpos($data,".",$strlocation);
else
{
$textend=$strlocation; $strlocation=$strlocation-100;
$search_result.="<p class='contenttext'> <a href='$link'> $phpfilename </a><br />.... "
.substr($data, $strlocation-$i,$textend)."...</p>";
}
}
}
} //if end of if for checking if string is empty
if($search_result!="")
echo $search_result;
else
echo "<br /> No Result found on this search or bad input. <br />";
echo "</div> </div> </div>";
?>

 

 

 

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: