SitePoint Sponsor

User Tag List

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

    Boolean search help

    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;
        } 

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Didnt I answer this like... 4 pages ago? In fact, isnt that my code?

    What's the string supposed to look like after you input something with quotes in it?

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,045
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Didnt I answer this like... 4 pages ago? In fact, isnt that my code?

    What's the string supposed to look like after you input something with quotes in it?
    Very possibly yes, just isn't working as I require. Sorry about that.

    When you input something in quotes it should just keep the quotes around it so that I can search for that exact phrase.

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    The code listed above should not consume quotation marks....

    Code:
    echo booltostring('This "that the" not other');
    Output:
    +this "that the" -other

  5. #5
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,045
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    The code listed above should not consume quotation marks....

    Code:
    echo booltostring('This "that the" not other');
    Output:
    +this "that the" -other
    Hi,

    Ok, but have you checked the output if you use "fuel systems OR engine OR common rail" as the input?

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    so you're saying you want to ignore the keywords if they exist inside quotes?

  7. #7
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,045
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    so you're saying you want to ignore the keywords if they exist inside quotes?
    No, if the keywords are put in quotes, it should just return the input (with quotes around it).

    So if I enter, "test test1 test 2" it should just return "test test1 test2"

    Does that make sense?

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    It should return +"test test1 test2", you mean. And it does....

  9. #9
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,045
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    It should return +"test test1 test2", you mean. And it does....
    Ok....but what about when you enter:

    fuel systems OR engine OR common rail

    What does it return for you?

  10. #10
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    +fuel systems engine "common rail"

    which... isnt quite right. The default case should be rewritten in this case as
    PHP Code:
                    default:
                     if(
    count(explode(' ',$forms[$i])) > 1) {
                        
    $out .= (($plusflag) ? "+" "").'"'.$forms[$i].'" ';
                     } else {
                        
    $out .= (($plusflag) ? "+" "").$forms[$i]." ";
                    }
                    break; 
    Which then outputs the expected
    +"fuel systems" engine "common rail"


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
  •