Cronthenoob Posted August 18, 2006 Share Posted August 18, 2006 How do I remove spaces from something a user typed in?If they type "hello world" into the text box and hit submit, how would i make it submit as "helloworld"??php.net doesnt like me and I can't really find anything on that site ever. I've used this before but can't remember what it is. Link to comment Share on other sites More sharing options...
dcole.ath.cx Posted August 18, 2006 Share Posted August 18, 2006 <?php$str = 'hello world';$str = preg_replace(' ', '', $str);// This will be 'helloworld' nowecho $str;?> just replace 'space' with nothing, to remove spaces... Link to comment Share on other sites More sharing options...
Cronthenoob Posted August 18, 2006 Author Share Posted August 18, 2006 I got this error when I used that:Warning: preg_replace() [function.preg-replace]: Empty regular expression in C:\Sites\page.php on line 13 Link to comment Share on other sites More sharing options...
ThePsion5 Posted August 18, 2006 Share Posted August 18, 2006 To turn multiple spaces into one space, you'll need a regular expression like this: $RegExp = '%[\s]{2,}%';$Text = preg_replace($RegExp, ' ', $Unfiltered); Hope it helps EDIT: Oh, i missunderstood what you wanted to do. Instead of using a regular expression, this should work just as well: $Text = str_replace(' ', '', $Text) That will give you what you need, and it's plenty faster than regular expressions too. Link to comment Share on other sites More sharing options...
Cronthenoob Posted August 18, 2006 Author Share Posted August 18, 2006 To turn multiple spaces into one space, you'll need a regular expression like this:$RegExp = '%[\s]{2,}%';$Text = preg_replace($RegExp, ' ', $Unfiltered); Hope it helps EDIT: Oh, i missunderstood what you wanted to do. Instead of using a regular expression, this should work just as well: $Text = str_replace(' ', '', $Text) That will give you what you need, and it's plenty faster than regular expressions too. Thanks! That works. Link to comment Share on other sites More sharing options...
aspnetguy Posted August 18, 2006 Share Posted August 18, 2006 use this regexp to filter spaces $newString = eregi(" *", $string); It should remove all spaces from the string Link to comment Share on other sites More sharing options...
reportingsjr Posted August 18, 2006 Share Posted August 18, 2006 How about trim($str) ? Wouldnt that take off spaces.. Link to comment Share on other sites More sharing options...
aspnetguy Posted August 18, 2006 Share Posted August 18, 2006 How about trim($str) ? Wouldnt that take off spaces..trim only removes spaces from the beginning and end of the string, not in the middle Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now