[ciapug] Search tool

Carl Olsen carl.olsen at DRAKE.EDU
Fri Jul 21 09:52:19 CDT 2006


I'm working on a simple search tool and I've got it splitting the search
into array elements based on double quotes.  The next step is to figure out
what to do with the array elements.  Here's what I'm thinking:

1.	If the words "and" or "or" are not found, then rank the elements in
the order they appear.  The highest ranking document would be a document
that has the highest proportion of the first element, followed by the
highest proportion of the second element, and so on.
2.	If the word "or" appears, then the two elements on either side of it
are ranked the same in order of priority, instead of the first element
having higher priority than the second.
3.	If the word "and" appears, then the two elements on either side of
it are both required and rank as one element in determining priority.

Has anyone done this? Would you be willing to share your code?

Here's my code, so far:

<?php
	if (isset($_POST['submit']))
	{
		$error = "";
		// remove any white space from the beginning or end of the
search string
		$search_string = trim($_POST['search']);
		// convert any smart quotes to normal quotes
		$search_string =
preg_replace('/\x93|\x94/','"',$search_string);
		// count to make sure there are an even number of quotes
		$quote_count = substr_count($search_string,"\"");
		// if there are not an even number of quotes, stop and
display an error
		// and repopulate the search textbox with the adjusted
search string
		$adjusted_search_string = htmlentities($search_string);
		if ($quote_count % 2 != 0)
		{
			$error = "unclosed quote";
		}
		// if there are no errors, continue on
		if($error == "")
		{
			$z = 0;
			$quotes = array();
			$zz = 0;
			$quotes_adjusted = array();
			// split the search on the quote characters
			$quotes = explode("\"",$search_string);
			// remove any white spaces from the array elements,
			// mark the elements of the array that are inside
quotes,
			// and remove any empty or null elements from the
array
			for ($z; $z < sizeof($quotes); $z++)
			{
				if (trim($quotes[$z]) != "" && $quotes[$z]
!= null)
				{
					$quotes_adjusted[$zz]['string'] =
trim($quotes[$z]);
					if ($z % 2 == 1)
					{
	
$quotes_adjusted[$zz]['quote'] = "true";
					}
					else 
					{
	
$quotes_adjusted[$zz]['quote'] = "false";
					}
					$zz++; 
				}
			}

			// check to make sure there are elements in the
array
			// after it has been adjusted, and break the
non-quoted elements
			// into arrays
			$unquoted_elements = array();
			$search_elements = array();
			if (sizeof($quotes_adjusted) > 0)
			{
				$y = 0;
				$yy = 0;
				for ($y; $y < sizeof($quotes_adjusted);
$y++)
				{
					if ($quotes_adjusted[$y]['quote'] ==
"false")
					{
	
$quotes_adjusted[$y]['string'] = trim(preg_replace("( +)","
",$quotes_adjusted[$y]['string']));
						$unquoted_elements =
explode(" ",$quotes_adjusted[$y]['string']);
						foreach($unquoted_elements
as $sub)
						{
	
$search_elements[$yy] = trim($sub);
							$yy++;
						}
					}
					else 
					{
						$search_elements[$yy] =
$quotes_adjusted[$y]['string'];
						$yy++;
					}
				}
			}
			print("<pre>");
			print($adjusted_search_string);
			print("</pre>");
			print("<pre>");
			print_r($quotes_adjusted);
			print("</pre>");
			print("<pre>");
			print_r($search_elements);
			print("</pre>");
		}
	}
?>
<div><?php print($error); ?></div>
<form id="form1" name="form1" action="search_test.php" method="post">
<input type="text" id="search" name="search" value="<?php
print($adjusted_search_string); ?>" />
<input type="submit" id="submit" name="submit" value="submit" />
</form>



More information about the ciapug mailing list