NYCPHP Meetup

NYPHP.org

[nycphp-talk] Regular expression for Google-like search

Stephen Musgrave stephen at musgrave.org
Mon Oct 10 16:11:10 EDT 2005


I searched the web and was shocked not to find the answer to this.... 
so I bring it to "the list".

I am building content management tool, within which I want people to be 
able to search for content.  I want to use the same query string rules 
as Google does - specifically the quote to group words into a unified 
string.

For example:

"the color purple" movie soundtrack

"the color purple" would need to be an exact match, ignoring the spaces 
as OR characters.  The spaces outside the quotes would still be treated 
like an OR character.

I am using this code so far:

$search_string = $_REQUEST['search_string'];
$search_terms = array();
$search_terms = preg_split('/"/', $search_string, -1, 
PREG_SPLIT_NO_EMPTY);

And it results in:

Array ( [0] => the color purple [1] => movie soundtrack )

But I want it to result in:

Array ( [0] => the color purple [1] => movie [2] => soundtrack )

I feel like I'm a couple keystrokes away... any help is much 
appreciated.

Thanks,

Stephen




More information about the talk mailing list