Take the spaces out of a form

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
jbcom41
Forum Newbie
Posts: 3
Joined: Tue Jul 25, 2006 3:24 pm
Location: Scotland

Take the spaces out of a form

Post by jbcom41 »

Hi everyone

Can someone give a hand to a Scots man who is totally new to php?

All I want to do is take out all the spaces in a form that is returned. I know how to remove the white spaces from the beginning and end but how do you remove the ones in the middle of the text and numbers that are returned?

Any help would be greatly appreciated
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

trim() to remove excess whitespace around edges of strings, and str_replace() for within the string.
jbcom41
Forum Newbie
Posts: 3
Joined: Tue Jul 25, 2006 3:24 pm
Location: Scotland

Hi Scart

Post by jbcom41 »

Hi there

I have a form that returns a phone number but some people put a gap in between the regional code and home number.

So I am trying to take that gap out so the info before it is inserted into the database like this 0129447097 in stead of like this 01294 470907!

Any help would be great!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Jcart gave you a solution. Since this is in Regex, what have you tried as far as patterns?
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

As jcart said...

Code: Select all

str_replace(' ', '', $phoneNumber)
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

jamiel wrote:As jcart said...

Code: Select all

str_replace(' ', '', $phoneNumber)
I dont really like that. You are only looking for spaces. It would probably better to remove anything that is not a number... and since this is in the regex section...

Code: Select all

$phoneNumber = preg_replace('/[^\d]/', '', $phoneNumber);
jbcom41
Forum Newbie
Posts: 3
Joined: Tue Jul 25, 2006 3:24 pm
Location: Scotland

bokehman

Post by jbcom41 »

Hi bokehman

thanks for that, very mcuh appreciated, would buy you a whisky if you were in Scotland!

Jbcom41
Post Reply