search issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

search issue

Post by sulen »

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 :

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);
  }
<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>

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>&nbsp;<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>";
Any help will be appreciated. Thanks

Sulen
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

you have to explode the search key words, i am useing this in my current project, where keywords end with a comma.

Code: Select all

<?php
$keyword = "key1,key2,key3";
$explode = explode(",", $keyword);
$query = " AND (";
for($x=0; $x <sizeof($explode); $x++)
{
$query .= "`field` LIKE '%$explode[$x]%' ";
}
if$x < (sizeof($explode)-1))
{
$query .= "OR";
}
?>
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Post by sulen »

I made this change to my code

Code: Select all

$search=explode(' ', $string); // explode info 
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);
  } 

foreach ($search as $search_term) { // foreach info 
search_dir ($cur_path, $dir_depth, $matches);   
search_file ($file);
<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>&nbsp;<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>";
}

But now i am getting this error :

- Warning: fread(): supplied argument is not a valid stream resource
- Warning: fclose(): supplied argument is not a valid stream resource
- Warning: strstr(): Empty delimiter.

This damn thing is killin me............
Post Reply