[nycphp-talk] Regular expression for Google-like search
Dan Cech
dcech at phpwerx.net
Mon Oct 10 16:34:51 EDT 2005
Stephen,
For this kind of a problem it may be simpler (and possibly more
efficient considering the small size of the strings) to write a simple
parser that goes through the string and builds up the query that way,
detecting whether you are inside a quoted string or not and behaving
appropriately as well as potentially processing AND OR NOT - (a,b,c),
etc constructs.
It seems like this could be a very worthwhile project, and more than
likely there is already something out there I'm not aware of to solve
this common problem.
You might also want to try digging into the source of some of the more
mature wiki or cms applications for inspiration.
Dan
Stephen Musgrave wrote:
> 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