Hello,
Here is the code that searches through the file I describe in my other post, also include are some other functions I made to make searching through files easier. Keep in mind that some of the code may be bloted due the time I had to finish it. I hope other people find this code helpfull. Let me know if anyone has any suggestion for the memory hording problem(this code is not where the problem occurs).
/*
** Sift through file, looking for files containing a keyword
*/
function sift_file($filename, $str)
{
$keywords = $this->keywords;
$kCount = count($keywords);
$occur = 0;
$int = 0;
$found = 0;
$ary_tmp = array();
for($i = 0; $i < $kCount; $i++)
{
while( stristr($str, $keywords[$i]) != "" )
{
$sTmp = $str;
$pos_start = strpos($sTmp, $keywords[$i]);
$pos_end = strlen($keywords[$i]);
$str = substr($sTmp, 0, $pos_start);
$sTmp = substr($sTmp, ($pos_start + $pos_end));
$str = ($str . $sTmp);
$found = 1;
$occur++;
}
if( $found )
{
$ary_tmp[$int] = $keywords[$i];
$int++;
$found = 0;
}
}
if( $occur != 0 )
{
$str = "";
for($n = 0; $n < count($ary_tmp); $n++)
$str .= $ary_tmp[$n] . "~";
/***-----[ Whimpy atempt of using relavance ranking
*/
if( count($ary_tmp) == count($keywords) )
$occur = ( (333 * pow( $occur, $occur)) + $occur);
$this->ary_file[$this->count] = ($filename . "~" . $str . $occur);
$this->count++;
}
}
/*
** Converts the contents of a file into a string
*/
function file2str($filename)
{
$aFile = file($filename);
$str = implode(" ", $aFile);
$pos_start = strpos($str, "<body");
$str = substr($str, $pos_start);
return($str);
}
/*
** Removes duplicate elements out of an array
*/
function clean_dual($array)
{
$count = count($array);
$aTmp = array();
$int = 0;
for($i = 0; $i < $count; $i++)
{
if($array[$i] != "")
{
$aTmp[$int] = $array[$i];
$int++;
}
for($n = ($i + 1); $n < $count; $n++)
{
if($array[$i] == $array[$n])
{
$array[$n] = "";
}
}
}
return($aTmp);
}
/*
** Strip Title from file
*/
function get_title($filename)
{
/***-----[ STRIPS TITLE FROM FILE, IF NO TITLE IS FOUND
****-----[ RETURNS A STRING WHICH CONTAINS THE FILE NAME.
*/
$filename = $this->clean_str($filename);
$filename = str_replace(" ", "", $filename);
$aFile = file($filename);
$str = implode(" ", $aFile);
$sTmp = strtolower($str);
if( strstr($sTmp, "<title>") != "")
{
$pos_start = ( strpos($sTmp, "<title>") + 7);
$pos_end = strpos($sTmp, "</title>");
$str = substr($str, $pos_start, ($pos_end - $pos_start));
if( strlen($str) <= 1 )
{
$str = str_replace($this->url, "", $filename);
while( stristr($str, "/") != "" )
{
$pos = strpos($str, "/");
$str = substr($str, ($pos + 1));
}
}
}
else
{
$str = str_replace($this->url, "", $filename);
while( stristr($str, "/") != "" )
{
$pos = strpos($str, "/");
$str = substr($str, ($pos + 1));
}
}
if( strlen($str) > TITLE_LENGTH )
{
$str = substr($str, 0, TITLE_LENGTH);
$str .= "...";
}
if( stristr($str, " ") != "" )
{
$ary_tmp = explode(" ", $str);
for($i = 0; $i < count($ary_tmp); $i++)
{
$string = $ary_tmp[$i];
$string = (strtoupper($string[0]) . substr($string, 1));
$ary_tmp[$i] = $string;
}
/*
** Cleans Carriage Returns Out Of Elements
*/
function clean($array)
{
$count = count($array);
for($i = 0; $i < $count; $i++)
{
$array[$i] = stripcslashes($array[$i]);
$array[$i] = str_replace(chr(10),"",$array[$i]);
$array[$i] = str_replace(chr(13),"",$array[$i]);
}
return($array);
}
/*
** Cleans Carriage Returns Out Of String
*/
function clean_str($string)
{
$string = stripcslashes($string);
$string = str_replace(chr(10),"",$string);
$string = str_replace(chr(13),"",$string);
return($string);
}
$str = implode(" ", $ary_tmp);
}
else
$str = (strtoupper($str[0]) . substr($str, 1));
return($str);
}