I'm developing a small flexible search tool for the news section at Zeropaid.com. I have already most of the search built, but I'm attempting to add one small feature to it.
Results post title of story and URL. What I want it to do is display 25 chars before and after the keyword. Here is a snipit of the code. Please let me know if you want me to post other code.
//########################### FUNCTION Get_Surrounding #######
function get_surrounding($filename)
{
$open_file = fopen($filename, "r");
$contents = fread($open_file,9000000);
fclose($open_file);
$contents = strip_tags($contents);
$start = strpos(" ",$keyword - 1);
$length= strpos(" ",$keyword) + strlen($keyword) + 300;
$retval = substr($contents,$start,$length);
return $retval;
}
//###########################
if ( isset($keyword) ) // Determine whether keyword is set, it is.
{
$keywords = explode(" ", $keyword);
$pages = array();
$titles = 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);
for($i = 0;$i < count($found); $i++)
{
$add = "$val$found[$i]";
$pages[count($pages)] = $add;
$titles[count($pages)-1] = Get_Title($add);
$titles_surround[count($pages)-1] = Get_Surrounding($add);
}
}
?>