Page 1 of 1

Combination of keywords

Posted: Thu Apr 01, 2004 12:02 pm
by sulen
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]

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

Posted: Thu Apr 01, 2004 1:52 pm
by TheBentinel.com
What did you want to do with the split() function? The man page for it can be found at http://www.php.net/split

Posted: Thu Apr 01, 2004 1:57 pm
by sulen
I need to modify the code to accept a combination of keywords separated with the '+' symbol.

Posted: Thu Apr 01, 2004 2:31 pm
by TheBentinel.com
split() will take your search phrase and split each word into an element of an array. Then you can foreach through the array. Set a flag before your foreach to "True", meaning all search words were found. If you can't find one of the words, set the flag to false. (And you might as well break out of the foreach at that point, too) After the foreach, check the flag. If its still True, then all the words were found in the file.

Watch your use of "if ($variable = 'value')" since the single = will assign 'value' to $variable and return True, even if it wasn't set to 'value' to start with. Maybe that's your intention, but I wanted to make sure. If all you're trying to do is test the value of $variable, use "if ($variable == 'value')"

Posted: Thu Apr 01, 2004 4:18 pm
by sulen
I tired to modify the code by adding the split functions inside but still it does not work. All it does it display 'Array' and searches for the letters in the word 'Array'. This is driving me insane. Any help will be appreciated

Admin Edit: Added tags to the code to make it easier to read - please use these tags and help us help you.[/color][/b]

Code: Select all

<?

$search_term = split(" \+", $string);
$num_keywords = count ($search_term);
//$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;

for ($i=0; $i<$num_keywords; $i++) {

$f_dlc    = strtolower($f_data);
$t_text   = ""; 
$in_tag   = 0;
  if ($text = strstr(strip_tags($f_dlc), $search_term[$i])) {
    $results[$matches] = $file;
    $text = substr($text, 0, 200);
    $text = str_replace ($search_term[$i], "<font color="#127A84"><b>".$search_term[$i]. "</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>

<?

$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>";
?>
</div>

Posted: Fri Apr 02, 2004 2:49 am
by twigletmac
sulen - please use the tags for your code.

Mac