SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:
    Code:
    pilot not sales
    would become:

    Code:
    +pilot -sales

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $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); 

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    PHP Code:
    $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:

    Code:
    pilot and international pilot not commercial
    this would need to be converted to:

    Code:
    pilot "international pilot" -commercial

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    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?

  5. #5
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code:
    pilot and international pilot not commercial or virgin airways
    would become:

    Code:
    pilot "international pilot" -commercial "-virgin airways"

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    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>

  7. #7
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    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.

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?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;
      }
    }

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Searched Google Code (with switch lang : php) and found boolTok.php which might be of interest.

  10. #10
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,048
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    PHP Code:
    <?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;
      }
    }
    Great job, I think it does what I need! :-) Thank you


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •