Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to split a search term

Status
Not open for further replies.

benrob82

Programmer
Jul 28, 2005
116
GB
Hi, I need to be able to split a search terms and then use it to query the database. If i enter one search term it works fine but I when two are enetered it doesnt work.

Do I use explode() or preg_match() can someone please help.
 
Explode could be used. However explode needs a separator character to split the string.

Supposing your separator character is a space:

Code:
$searchterms=explode(" ",$_POST['forminput']);

This will then give you an array with the keywords.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I would use preg_split():

$searchterms = preg_split ('/\s+/',$_POST['forminput']);

That way, if your user enters multiple spaces between words, you don't get empty array elements.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top