Re: Regex for Advanced search feature

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 2007-10-19 at 09:48 -0700, Kevin Murphy wrote:
> I'm trying to create an advanced search feature for my site, and I  
> have it mostly working the way I want. I take whatever search term  
> ($searchkey) that the user submits, explodes if off any spaces  
> between words, and then use that to search for each word separately.
> 
> $keys = explode(" ",$searchkey);
> 
> So the search phrase of:
> 
> Dog Cat Horse
> 
> would output :
> 
> Array
> (
>      [0] => Dog
>      [1] => Cat
>      [2] => Horse
> )
> 
> However, I would like to add the ability to have phrases in there  
> indicated by " marks. So, if the search phrase was this:
> 
> "Dog Cat" Horse
> 
> It would output:
> 
> Array
> (
>      [0] => Dog Cat
>      [1] => Horse
> )
> 
> My concept to solve this is right before I explode the keys as above,  
> to replace any spaces within " marks with a garbage string of  
> characters (say XXXXXXXXXX) using a regular expression, and then  
> later replace it back with a space. However, I suck at regular  
> expressions and can't figure it out.
> 
> Anyone have a way for me to accomplish this with a regular expression  
> or with another method?

<?php

$foo = '"dog cat" horse mouse "tiger fish"';

if( preg_match_all( '/[[:space:]]+(("[^"]*")|([^[:space:]]+))/', '
'.$foo, $bits ) )
{
    print_r( $bits );
}

?>

Cheers,
Rob.
-- 
...........................................................
SwarmBuy.com - http://www.swarmbuy.com

    Leveraging the buying power of the masses!
...........................................................

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux