Simple static content search using PHP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The following page contains a simple static content search I put together for a website this morning quite quickly - in under an hour total time:
https://dl.dropbox.com/u/13327195/intergritas/search.php

And the two parts of the page that contain code are the following - added comments in here:
<?php
//this function is the only bit of code went looking for on 'net and copied in here
function page_title($url) {
$fp = file_get_contents($url);
if (!$fp) {
return null;
}
$res = preg_match("/<title>(.*)<\/title>/siU", $fp, $title_matches);
if (!$res) {
return null;
}

// Clean up title: remove EOL's and excessive whitespace.
$title = preg_replace('/\s+/', ' ', $title_matches[1]);
$title = trim($title);
return $title;
}//end of page_title function

//create empty array to then populate
$arResults = array();
//check if search form was submitted
if (isset($_POST["q"])) {
//split search terms up into array using space character
$arTerms = explode(" ", $_POST["q"]);
//retrieve an array of all .php files to search in site root
$arFiles = glob("*.php");
//loop through resulting file names
foreach ($arFiles as $file) {
//exclude two purely functional file names
if ($file != "mailer.php" && $file != "search.php") {
//retrieve file contents, and strip them of all tag/code content line by line
$f = fopen($file, "r");
$sBody = "";
while ($sLine = fgetss($f)) {
$sBody .= $sLine . " ";
}//end of looping through file contents
//split file body content into it's own array based on space characters
$arBody = explode(" ", $sBody);
//compare search term array with each files contents, and if there are matching terms, add file to result array
if (count(array_diff($arTerms, $arBody)) < count($arTerms)) {
array_push($arResults, $file);
}//end of checking for terms matching body
}//end of excluding certain files
}//end of looping through files
//initialise new result output array
$arResultsOutput = array();
//if there were matching pages, add them to output array
if (count($arResults) > 0) {
foreach ($arResults as $result) {
//push them into arResultsOutput array with file and title key-value pairs
array_push($arResultsOutput, array("file"=>$result, "title"=>page_title($result)));
}//end of looping through arResults
}//end of processing resulting file names
}//end of checking for form submission
?>

//and the piece of the page output that renders the search results:
<?php
//output search results - if arResultsOutput array has elements in it
if (count($arResultsOutput) > 0) {
echo "<ul>\n";
//for each resulting array element
foreach ($arResultsOutput as $result) {
echo "<li><a href=\"" . $result["file"] . "\">" . $result["title"] . "</a></li>\n";
}//end of looping through arResultsOutput
echo "</ul>\n";
} else {
echo "No search results found";
}
?>

Stay well

Jacob Kruger
Blind Biker
Skype: BlindZA
'...fate had broken his body, but not his spirit...'

[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux