View Single Post
  #1 (permalink)  
Old 03-04-2010, 07:14 AM
bodyboard662 bodyboard662 is offline
Junior Member
 
Join Date: Mar 2010
Posts: 10
Default Is preg_split() the best choice for this?

Working on search engine functionality and I'm trying to handle queries that contain matched quotes.

Example:
input: foo , output: " AND x LIKE '%foo%'"
input: foo bar , output: " AND x LIKE '%foo%' AND x LIKE '%bar%'"
input: "foo bar" , output: " AND x LIKE '%foo bar%'"

At the moment I'm splitting the search string by a space then looping through each word and just building a straight up "AND x LIKE..." string which gets appended to the final query I run. It takes a lot of logic just using explode() to handle the quotes correctly, there has to be a better way.

Perhaps using preg_split() would be a better alternative? I don't use regexs too often so I'm not really sure if this is even possible, thoughts?

The end result would be getting an array of terms. In the above example the array should be:

0 = foo
1 = foo
2 = bar
3 = foo bar

Or if this is more clear:

User searches for: foo bar

Array would be:

0 = foo
1 = bar

User searches for: "foo bar"

Array would be:
0 = foo bar

User searches for: foo "foo bar"

Array would be:
0 = foo
1 = foo bar
__________________
Bodyboarding | Bodyboarding
Reply With Quote