How to develop a page search function in php
Posted by Shan
This function is developed using simple php. This will not work in dynamic contents because it’s searches the file; line by line in the server end. If you have a website with lots of static html pages I think you will find this very useful. One of the down side is even if you have a html comment in the file it’ll search that as well.
HTML Form:
<form action=”test_search.php” method=”post” name=”d”>
<input name=”send” type=”hidden” value=”1″ />
<input name=”term” type=”text” />
<input name=”submit” type=”submit” value=”submit” />
</form>
PHP Code:
<?php
//this fucntion is used to get the tile tag of the page
function get_title($a,$b,$c){
$y = explode($b,$a);
$x = explode($c,$y[1]);
return $x[0];
}
//if form is submited do the following
if(isset($_POST['send'])){
if ($handle = opendir(‘/path to your directory/’)) { //example path /home/site-user-name/public_html/my-folder/
// lets loop over the directory.
$i=0;
while (false !== ($file = readdir($handle))) {
if ($file != “.” && $file != “..”) {
$page= ‘myfolder/’.$file;
$page= file ($page);
$numtlines = count ($page); //Number of lines in the file
$line = 0;
while ($line < $numtlines) {
if(stristr ($page[$line], ”.$_POST['term'].”)){ //found a match
//let’s get the page title
echo ‘<br><a href=”http://www.url-to-your-file/’.$file.’”>”‘.get_title(file_get_contents(‘http://www.url-to-your-file/’.$file), “<title>”, “</title”).’</a><br>’;
$metatagarray = get_meta_tags(‘http://www.url-to-your-file/’.$file);
echo $description = $metatagarray[ "description"];
$line = $numtlines;
}
$line++;
}
}
$i++;
}
closedir($handle);
}
}
?>