I'm trying to convert a user entered string into a MySQL boolean search. I have the following function which works perfectly for a number of scenarios, however it doesn't work with double quotes.
So for example, it WORKS with the following strings:
String1: technical not manager String2: technical or manager or admin
The only one that's not working right now, is when you enter a phrase enclosed within double quotes. For example:
String3(doesn't work): "technical writer" not "document manager"
Here's the function i'm using:
function booltostring($input)
{
$input = strtolower($input);
$out = "";
$plusflag = false;
$minusflag = false;
$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;
$minusflag = false;
if(count(explode(' ',$forms[$i+1])) > 1)
{
$out .= '"+'.$forms[$i+1].'" ';
}
else
{
$out .= "+".$forms[$i+1]." ";
}
$i++;
break;
case 'or':
if(strpos($forms[$i-1],'OR')>-1 || strpos($forms[$i-1],'or')>-1)
{
$plusflag = true;
}
if(count(explode(' ',$forms[$i+1])) > 1)
{
$out .= '"'.(($minusflag) ? "-" : "").$forms[$i+1].'" ';
}
else
{
$out .= (($minusflag) ? "-" : "").$forms[$i+1]." ";
}
$i++;
break;
case 'not':
$plusflag = false;
$minusflag = true;
if(count(explode(' ',$forms[$i+1])) > 1)
{
$out .= '"-'.$forms[$i+1].'" ';
}
else
{
$out .= "-".$forms[$i+1]." ";
}
$i++;
break;
default:
if(strpos($forms[$i+1],'OR')>-1 || strpos($forms[$i+1],'or')>-1)
{
$plusflag = false;
}
if(strpos($forms[$i+1],'AND')>-1 || strpos($forms[$i+1],'and')>-1)
{
$plusflag = true;
}
if(count(explode(' ',$forms[$i])) > 1)
{
$out .= (($plusflag) ? "+" : "")."\"".$forms[$i]."\" ";
}
else
{
$out .= (($plusflag) ? "+" : "").$forms[$i]." ";
}
break;
}
}
$out = trim($out);
return $out;
}