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

String Filtering

Status
Not open for further replies.

alsaffar

Programmer
Oct 25, 2001
165
KW
Hi there,

If I have a text box that the user will enter keywords to search a databse, then I want to filter his string according to the follwoing:

1. At least there is one non space character in the string.
2. No more than 1 space between the words in the string.
3. No space at the end of the string.
4. Make the string all lower case.

Lets say the string is: "I Want to be a PILOT "

How can I achieve my goal?
 
1. At least there is one non space character in the string.
2. No more than 1 space between the words in the string.
3. No space at the end of the string. - trim
4. Make the string all lower case. strtolower()

maybe for the other two you need to use explode() then trim and then implode with a space as the deliminator
Sometimes, when my code just won't behave, I take it outside and make it listen to britney spears music, and when it comes back it's really well behaved. I wonder if it's suffering from post tramatic stress syndrome now..
 
[tt]if (preg_match('/[A-Za-z0-9]/', $string)) /* There is at least one alphanumeric character */
{
$search = array('/ +/', /* Replace all occurances of multiple spaces with one */
'/[\r\n\t ]+$/', /* Remove trailing space characters */
'/([A-Z])/e'); /* Replace capital letters */
$replace = array(' ', '', 'strtolower($1)');
$newstring = preg_replace($search, $replace, $string);
}[/tt]
This is somewhat overkill, but does what you want. Refer to the manual for more info about it. //Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top