Boolean search help

Hi Guys!

I have built a boolean search function which should parse user inputted text string and output the correct boolean query to send to MySQL.

However, when a user inputs fuel systems OR engine OR common rail it should return the following output:


fuel systems engine common rail

If the user inputs fuel systems AND engine then it should return the following output:

+fuel systems +engine

If the user inputs fuel systems NOT engine then it should return the following output:

+fuel systems -engine

If the user inputs “fuel systems” (in quotation marks) then these words must appear next to each other in this sequence.

Function:


function booltostring($input){
		$input = strtolower($input);
		$out = "";
		$plusflag = true;
		$forms = preg_split("%(and|or|not)%",$input,-1,PREG_SPLIT_DELIM_CAPTURE);
		$forms = array_map('trim',$forms);
		
		for($i = 0; $i < count($forms); $i++) {
		  switch($forms[$i]) {
			   case "and":
				 $plusflag = true;
				 if(count(explode(' ',$forms[$i+1])) > 1) {
					$out .= '"+'.$forms[$i+1].'" ';
				 } else {
					$out .= "+".$forms[$i+1]." ";
				 }
				 $i++;
			   break;
			   case 'or':
				 if(count(explode(' ',$forms[$i+1])) > 1) {
					$out .= '"'.(($plusflag) ? "" : "-").$forms[$i+1].'" ';
				 } else {
					$out .= (($plusflag) ? "" : "-").$forms[$i+1]." ";
				 }
				 $i++;
			   break;
			   case 'not':
				 $plusflag = false;
				  if(count(explode(' ',$forms[$i+1])) > 1) {
					$out .= '"-'.$forms[$i+1].'" ';
				  } else {
					$out .= "-".$forms[$i+1]." ";
				  }
				  $i++;
				break;
				default:
					$out .= (($plusflag) ? "+" : "").$forms[$i]." ";
				break;
		  }
		}
		$out = trim($out);

		return $out;
	}

Didnt I answer this like… 4 pages ago? In fact, isnt that my code?

What’s the string supposed to look like after you input something with quotes in it?

Very possibly yes, just isn’t working as I require. Sorry about that.

When you input something in quotes it should just keep the quotes around it so that I can search for that exact phrase.

The code listed above should not consume quotation marks…


echo booltostring('This "that the" not other');

Output:
+this “that the” -other

Hi,

Ok, but have you checked the output if you use “fuel systems OR engine OR common rail” as the input?

so you’re saying you want to ignore the keywords if they exist inside quotes?

No, if the keywords are put in quotes, it should just return the input (with quotes around it).

So if I enter, “test test1 test 2” it should just return “test test1 test2”

Does that make sense?

It should return +“test test1 test2”, you mean. And it does…

Ok…but what about when you enter:

fuel systems OR engine OR common rail

What does it return for you?

+fuel systems engine “common rail”

which… isnt quite right. The default case should be rewritten in this case as


                default:
                 if(count(explode(' ',$forms[$i])) > 1) {
                    $out .= (($plusflag) ? "+" : "").'"'.$forms[$i].'" ';
                 } else {
                    $out .= (($plusflag) ? "+" : "").$forms[$i]." ";
                }
                break;

Which then outputs the expected
+“fuel systems” engine “common rail”