#native_company# #native_desc#
#native_cta#

Boolean Keyword Search

By Trevor Linton
on September 6, 2001

Version: 1.0

Type: Function

Category: Algorithms

License: GNU General Public License

Description: This will format a boolean keyword search into an array that you can use to search any source.

<?		
	/*
		search.php by Trevor Linton
		
		BOOLEAN KEYWORD SEARCH FUNCTION
		This allows anyone to execute a boolean keyword search.  This can be used for a search engine of any type.  This function does not actually do the search but rather
		takes the users inputted data and formats it into an Array in which your own customized function can used to execute a SQL search, or grep files, etc.. Only one function
		exists called cutBoolean (with another function attach_array to help it).  They syntax is below along with how the array is structured that's returned.
		
		This supports grouping items with ( ), keywords AND, OR, NOT and quoted items " " it will also default anything without a keyword to an AND keyword. Such as, if you pass in
		'Linux PHP' the progarm will assume 'Linux AND PHP' and treat it as two keywords given, if you pass in: ' "Linux PHP" ' (basically the word enclosed with quotes) it will 
		correctly assume just one keyword "Linux PHP".
		
		Example:
		nursers AND malpractive NOT doctors 
		 
		Will return:
		x[0][0]["keyword"] = "nursers"
		x[0][0]["keyword"]= "malpractice"
		x[0][0]["attrib"] = "+"
		x[0][1]["keyword"] = "doctors";
		x[0][1][attrib"] = "-"
	
		x is the variable array
		The first array is the group
		The second array is the keyword in the group
		The ["keyword"] specifies the actualy text keyword (case sensetive)
		The ["attrib"] is either + or - for include or exclude in that group.
		
		to execute:
		
		cutBoolean(boolean_keywords string, level int, default_attrib char);
		
		level and default_attrib should always be initalized as 1 and '+'.
	*/

	
	function array_attach($dArray,$sArray) {
		// find the last id in $dArray
		$last_id = count($dArray);
		
		for($i=0; $sArray[$i] ; $i++) {
			$dArray[$last_id] = $sArray[$i];
			$last_id++;
		}
		
		return($dArray);
	}
	
	function cutBoolean($boolean, $level,$attrib) {

		$split = explode( " ",$boolean);
		$grpIndex = 0;
		$keyIndex = 0;
		$group = Array();
		
		for($i=0; $i < count($split) ;$i++) {

			// check for beginning group
			if($split[$i][0]=="(") {

				$tmp = "";
				
				$count = 0;
				
				$count +=  substr_count($split[$i], "(");
				$count -= substr_count($split[$i], ")");
				
				for($j=($i +1); $count != 0 ; $j++) {
					$count +=  substr_count($split[$j], "(");
					$count -= substr_count($split[$j], ")");
					
					if($j > count($split))
						break;
				}
				
				// now we have in $j where the group ends. concat all strings then return it as a group
				for($n=$i; $n != $j  ;$n++)
					$tmp .= " ".$split[$n];

				$tmp= ltrim(rtrim($tmp));

				if($tmp[0] == '(' && $tmp[strlen($tmp)- 1 ] == ')') 
					$tmp = substr(substr($tmp, 1, strlen($tmp)), 0,strlen($tmp) -2);
	
				// add this array to the end of the other!
	 			$group = array_attach($group, cutBoolean($tmp, ($level + 1), $attrib));
		
				$i=($j - 1);
				
			} else if($split[$i][0]=="" && $split[$i][1] == """) {

				$tmp = $split[$i];
				
				if($tmp[strlen($tmp) -1 ] != """) {
					for($j=($i+1);;$j++) {
						
						$tmp .= " ".$split[$j];
						if(substr_count($split[$j],'"') != 0)
							break;

					}
				
					$i=$j;
				} 
				
				
				$tmp = substr(substr($tmp, 2, strlen($tmp)), 0,strlen($tmp) -4);
				
				$group[$grpIndex][$keyIndex]["keyword"]  =$tmp;
				$group[$grpIndex]["attrib"] = $attrib;
				$keyIndex++;
			
			} else {
				switch($split[$i]) {

					case 'AND':
						if($not==true) {
 							$attrib='+';
							$not = false;
						}
						break;

					case 'OR':
						if($not==true) {
 							$attrib='+'; 
							$not = false;
						}
						$grpIndex++;
						$keyIndex = 0;
						break;

					case 'NOT':
						$not = true;
						$attrib = '-';
						$grpIndex++;
						$keyIndex = 0;
						break;
					case '':
						break;
					
					case ' ':
						break;

					default:
						if($not==true)	{
							$group = array_attach($group, cutBoolean($split[$i], ($level + 1), $attrib));
						} else {
							$group[$grpIndex][$keyIndex]["keyword"] = $split[$i];
							$group[$grpIndex]["attrib"] = $attrib;
							$keyIndex++;
						}
						break;
				}
				
			}
			
		}
		
		return($group);
	}
	

	echo("<b>DEBUG INFO<b><hr noshade size=0 width=100%>Query '$search'.</b><br><br>n");
	?>
	<table border="1" cellpadding="0" width="100%" cellspacing="3">
	<tr>
		<td><b>Group</b></td><td><b>Attribute</b></td><td><b>Keyword(s)</b></td>
	</tr>
	<?
	$groups = cutBoolean($search,0,'+');
	
	for($i=0 ; $groups[$i] ;$i++ ) {
		?>
	<tr>
		<td><?echo($i);?></td>
		<td><?echo($groups[$i]["attrib"]);?></td>
		<td>
			<?
					for($j=0; $groups[$i][$j] ; $j++) {
						echo("[".$j."] ".$groups[$i][$j]["keyword"]."<br>n");
					}
			?>
		</td>	
		<?
	}
	?>
	</table><br><br>