I am trying to figure out how to take create a search form that will allow users to type in keywords and then submit a query based on those keywords. Pretty much like every serch form out there. I can hanndle submitting the query but I can't figure out how to break the input string into the separate keyword values. I htink I could hack it out using regular expressions but I wanted to see if there is something available that might be more efficient than what I could come up with. Any suggestions would be greatly appreciated.
Thanks
$words_string=$_GET['stuff'];
You should convert the string into a n array using explode().
$words = explode(" ", $words_string);
I had to laugh! I was looking so hard I over looked the obvious. 2 questions though. Will I have to trim the values in the array or will explode ignore extra separators.( I suppose I could replace multiple spaces with a single space prior to explode.) Also was is the best way to allow for "multiple word keywords" contained in quotes as above. I want that to return a single value as opposed to three separate values. OK so, three questions then! Should I check for invalid data in the string before or after I explode it?
Thanks for the response!!
Replace spaces, tabs, and new-line characters with a single space:
$string="What's up ?"; $stripped = ereg_replace("[ \t\r\n]+", " ", $string);
You should check for invalid data also.