0

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;
}
Alan Moore
  • 73,866
  • 12
  • 100
  • 156

2 Answers2

0
"technical writer" not "document manager"

This string doesnt work because you are not removing the double quotes from the the string

remove them by using trim function

EDIT:

USE TRIM On Individual values i.e."technical writer", "document manager"

Like this trim('"document manager"','"');

Vipin Jain
  • 1,382
  • 1
  • 10
  • 19
0

You just need to remove the double quotes you are adding manually and then it should be no problem.

For example this one

$out .= '"+'.$forms[$i+1].'" ';

Because those double quotes are already in the input string

dan-lee
  • 14,365
  • 5
  • 52
  • 77