search issue
Posted: Mon Jun 28, 2004 2:25 pm
I have designed a search functionality on my intranet site which does the following
- takes a single word and finds its occurence in a directory and subdirectories
- returns the search result with the word highlighted and also proves the link of the file where the word is found.
But can somebody please tell me how do I modify the search to accept more than one keyword separated by a space or something eg
If I enter 'customer values' in my present search it searches for the occurence together and not as separate words. I need them to be separate words in the search..
The code is as follows :
<br><br>
<form action="<? echo $PHP_SELF; ?>" method="post">
<table width='100%' border='0' bordercolor='black' style='border: 1px' cellpadding='20' >
<tr>
<td class='a' align='right' width='50%'><b>Search the RFP Tool</b></td>
<td class='a' align='left' width='50%'><input type="text" name="string" size="40" maxlength="100" value="<? echo $search_term ?>" style='font-family: Verdana; font-size: 8pt; border-style: solid; border-width: 1; background-color: #ffffff'></td>
</tr>
<tr>
<td class='a' align='center' colspan='2'>
<input style='background-color:#ffffff; font-family: Verdana; font-color: #ffffff; font-size: 100%; border-color: #000000; border-width: 1' type='submit' value='Search RFP Tool'>
</td>
</table>
</form>
<br>
<ul>
<li><b>Important :</b> use exact words to obtain best search results</li>
</ul>
<table width='100%' border='1' bordercolor='black' style='border: 1px'>
<tr>
<td class='a' align="center" colspan=2>
Search Results For: <b><?echo $search_term; ?></b>
</td>
</tr>
<tr>
<td class='a' align="center" width="30%">Page</td>
<td class='a' align="center" width="70%">Page Text</td>
</tr>
Any help will be appreciated. Thanks
Sulen
- takes a single word and finds its occurence in a directory and subdirectories
- returns the search result with the word highlighted and also proves the link of the file where the word is found.
But can somebody please tell me how do I modify the search to accept more than one keyword separated by a space or something eg
If I enter 'customer values' in my present search it searches for the occurence together and not as separate words. I need them to be separate words in the search..
The code is as follows :
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);
}<form action="<? echo $PHP_SELF; ?>" method="post">
<table width='100%' border='0' bordercolor='black' style='border: 1px' cellpadding='20' >
<tr>
<td class='a' align='right' width='50%'><b>Search the RFP Tool</b></td>
<td class='a' align='left' width='50%'><input type="text" name="string" size="40" maxlength="100" value="<? echo $search_term ?>" style='font-family: Verdana; font-size: 8pt; border-style: solid; border-width: 1; background-color: #ffffff'></td>
</tr>
<tr>
<td class='a' align='center' colspan='2'>
<input style='background-color:#ffffff; font-family: Verdana; font-color: #ffffff; font-size: 100%; border-color: #000000; border-width: 1' type='submit' value='Search RFP Tool'>
</td>
</table>
</form>
<br>
<ul>
<li><b>Important :</b> use exact words to obtain best search results</li>
</ul>
<table width='100%' border='1' bordercolor='black' style='border: 1px'>
<tr>
<td class='a' align="center" colspan=2>
Search Results For: <b><?echo $search_term; ?></b>
</td>
</tr>
<tr>
<td class='a' align="center" width="30%">Page</td>
<td class='a' align="center" width="70%">Page Text</td>
</tr>
Code: Select all
$dir_depth=0;
$matches=0;
$cur_path = array("/");
$results = array("");
$r_text = array("");
$r_title = array("");
$search_term=strtolower($search_term);
if (strcmp($search_term, "")!=0) { search_dir(); }
$c="#ffffff";
for ($a=0; $a<$matches; $a++) {
if ($c=="#ffffff") {$c="#ffffff";} else {$c="#ffffff";}
echo "<tr><td class="a" align="center" bgcolor="$c">";
echo "<a class="five" href="$results[$a]" title="Go To This Page"><font color="#127A84">$r_title[$a]</font></a>";
// echo "<br> <code>$results[$a]</code>";
echo "</td><td class="a" align="left" bgcolor="$c">$r_text[$a]</td></tr>";
}
echo "</table><br><p>$matches Matches.<br><br>";Sulen