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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preg_split

Status
Not open for further replies.

brownfox

Programmer
Jan 5, 2003
173
GB
I have a search page that queries mySQL and prints the results. Here's the form:
<form name="form1" method="post" action="">
<input name="search-terms" type="text" id="search-terms">
<input type="submit" name="Submit" value="Submit">
</form>
But what if the user puts more than one term into the input box (search_terms)? I want to be able to split the words up put them into an array and use each on a query:
$search_terms = $_POST['search_terms'];
select from myTable where myCol = '$search_terms'
Any help much appreciated!
 
You could use the explode() function. This allows you to break a string up into pieces, seperated by some kind of character. So, for example, you could break a string up into pieces seperated by the spaces, thus getting each individual word as a result.

Here is how you use it:

Code:
$longstring = "THIS IS A STRING";
$pieces = explode(" ", $longstring);
echo $pieces[0]; // THIS
echo $pieces[1]; // IS
echo $pieces[2]; // A
echo $pieces[3]; // STRING

And there you have it. You can figure out some way to search the words individually through a for loop or something instead of just printing them like that script does.

The first argument of explode() is the thing you want to break it apart by...in this case a space. You could also use a comma or something. The second argument is just the string you want broken up.

Hope this helps! Post me back if it does!

Peace out,
Peace Co.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top