How can I get explode keywords for searching?

Discussion in 'PHP' started by goldensea80, Aug 10, 2007.

  1. #1
    OK, I already know the explode way:
    
    $keywords='One Two Three';
    $array=explode(' ',$keywords);
    // I will have array of: 'One', 'Two', 'Three'
    
    PHP:
    But incase Two or more word is double quoted
    
    $keywords='"One Two" Three';
    //$array=???
    // I will have array of 2 element: 'One Two', 'Three'
    
    PHP:
    How can I do that?
     
    goldensea80, Aug 10, 2007 IP
  2. RichardKnox

    RichardKnox Peon

    Messages:
    209
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    In the array they need to be seperated by a common factor. Eg:

    
    $keywords = 'One - Two - Three';
    $array = explode('-', $keywords);
    
    PHP:
    If your needing 'One Two' and 'Three' you could seperate like so:

    
    $array = '"One Two" - Three';
    
    PHP:
    But that would woutput "One Two" Three.
     
    RichardKnox, Aug 10, 2007 IP
  3. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Oh, I Just want to parse the key word like Google does. A string: "One Two" Three will be broken into One Two, Three. I guess some Regular Expression must be used? Can somebody help?
     
    goldensea80, Aug 10, 2007 IP
  4. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Yes, regex is the answer :)

    Try this:

    	$test = '"This is" a test';
    	$MyArray = array();
    	
    	$MyArray = preg_split( "/[\W]{1,4}/", $test );
    	
    	echo "<pre>";
    	print_r( $MyArray );
    	echo "</pre>";
    PHP:
    Brew
     
    Brewster, Aug 10, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    Not sure if there's a way to do that with a single regular expression, but this does work:

    
    <?php
    
    $query = 'This is what "I have" been "searching for!"';
    $keywords = array();
    
    while (preg_match('/(?:"([^"]+)"|([^\s]+))/', $query, $quotes))
    {
    	$keywords[] = trim(isset($quotes[2]) ? $quotes[2] : $quotes[1]);
    	$query = str_replace($quotes[0], null, $query);
    }
    
    echo '<pre>' . print_r($keywords, true) . '</pre>';
    
    ?>
    
    PHP:
     
    nico_swd, Aug 10, 2007 IP
    goldensea80 likes this.
  6. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #6
    Thanks, that works! :D


    @Brewster: Nope, thanks but it doesn't work
     
    goldensea80, Aug 10, 2007 IP
  7. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #7
    Hi goldensea80

    Just out of curiosity what string are you passing to my code? On my system the code outputs this:

    Array
    (
    [0] =>
    [1] => This
    [2] => is
    [3] => a
    [4] => test
    )

    Brew

    EDIT: Sorry, just re-read your initial question more closely... Nico is the winner! :)
     
    Brewster, Aug 10, 2007 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    He doesn't want to split the string on spaces if there are double quotes around it. Like Google does.

    This.
    
    $keywords='"One Two" Three';
    
    PHP:
    Sould become this:
    
    Array
    (
        [0] => One two
        [1] => Three
    )
    
    Code (markup):
     
    nico_swd, Aug 10, 2007 IP
  9. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #9
    Yeah, that's it!
    Anyway, thank to all of you for your kindness and your willing to help :)
     
    goldensea80, Aug 10, 2007 IP