PHP & MySQL boolean search

Hi Guys…

I am looking for a function of class that converts boolean string searches into a MySQL query.

For example:

pilot not sales

would become:

+pilot -sales
$words = explode(' ',$input);
$plus = true;
$out = "";
foreach($words AS $word) {
  if($word == "not") {
    $plus = false;
    continue;
  }
  $out .= (($plus) ? "+" : "-").$word." ";
  $plus = true;
}
echo substr($out,0,-1);

Thanks, but that function only replaces the words with + and -

I am looking for something more comprehensive as some queries may be like this:

pilot and international pilot not commercial

this would need to be converted to:

pilot "international pilot" -commercial

I gave you the answer for what you gave me.

So you need to define your transformation rules.
keyword: not
transformation rule: -<following word>

keyword: and
transformation rule: “<following word> <word following that>”
?

etc,etc etc…

Why does pilot in your last post not have + in front of it?

Sorry, there shouldn’t be a plus sign at all in the posts.

Also, there is also another keyword; “or”. The rule for that would follow the previous seperator, so for example:

pilot and international pilot not commercial or virgin airways

would become:

pilot "international pilot" -commercial "-virgin airways"

So let me make sure i’ve got you right:

keyword: and
transformation rule: “<next word> <one following word>”

keyword: or
transformation rule: “<previousmode><next word> <one following word>”

keyword: not
transformation rule: -<nextword>

Yes, this is correct, however please note the following:

keyword: and
transformation rule: “<next word> <one following word>”
^^ Note: Will only have quotes when there is more than one word and it will not always have one following word (may have a few).

keyword: or
transformation rule: “<previousmode><next word> <one following word>”
^^ Note: Will only have quotes when there is more than one word and it will not always have one following word (may have a few).

keyword: not
transformation rule: -<nextword>
^^ Note: May have to have quotes if there is more than one word (i.e. “-commercial pilot”

I hope this makes sense - I was searching on the net but can’t find anything that does this even though it must have been done many times before.

<?php
$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 .= $forms[$i]." ";
        break;
  }
}   

Searched Google Code (with switch lang : php) and found boolTok.php which might be of interest.

Great job, I think it does what I need! :slight_smile: Thank you