Combination of keywords
Posted: Thu Apr 01, 2004 12:02 pm
I have a search program in PHP that does an exact search on keyword/keywords in a directory and its corresponding subdirectories. I need to add more functionality such that it searches for a combination of keywords separated by the '+' symbol like in google. I am confused on how to use the split function. Any help will be appreciated. Thanks.
The code is as under:
Admin Edit: Added tags to the code to make it easier to read - please use these tags and help us help you.[/color][/b]
The code is as under:
Admin Edit: Added
Code: Select all
Code: Select all
$search_term = $string;
function search_dir () {
global $cur_path, $dir_depth, $matches;
if ($matches > 100) { return; }
$s_dir="rfp";
for ($c=0; $c<=$dir_depth; $c++) { $s_dir .= $cur_path[$c]; }
$dhandle=opendir("$s_dir");
while ($file = readdir($dhandle)) {
if (($file!=".") && ($file!="..")) {
if (is_file($s_dir.$file)) {
$file_ext = substr($file, strlen($file)-3, 3);
if ($file_ext == "php") search_file($s_dir.$file);
}
elseif (is_dir($s_dir.$file)) {
$cur_path[++$dir_depth] = ($file."/");
search_dir();
$dir_depth--;
}
}
}
}
function search_file ($file) {
global $cur_path, $dir_depth, $search_term, $results, $r_text, $r_title, $matches;
$s_dir="rfp";
for ($c=0; $c<=$dir_depth; $c++) $s_dir .= $cur_path[$c];
$f_size = filesize($file);
$f_handle = fopen($file, "r");
$f_data = fread($f_handle, $f_size);
if ($text = strstr($f_data, 'SSIGNORE')) return;
$f_dlc = strtolower($f_data);
$t_text = "";
$in_tag = 0;
if ($text = strstr(strip_tags($f_dlc), $search_term)) {
$results[$matches] = $file;
$text = substr($text, 0, 200);
$text = str_replace ($search_term, "<font color="#127A84"><b>".$search_term. "</b></font>", $text);
$r_text [$matches] = "...". $text. "...";
$t_start = strpos ($f_dlc, "<title>") + 7;
$t_end = strpos ($f_dlc, "</title>");
$r_title[$matches++] = substr($f_data, $t_start, $t_end-$t_start);
}
fclose($f_handle);
}
?>