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:
Code:
fuel systems engine common rail
If the user inputs fuel systems AND engine then it should return the following output:

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

Code:
+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:

PHP Code:
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;
    }