homepage Welcome to WebmasterWorld Guest from 54.146.175.204
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
ereg replace numbers and spaces
how to format a phone number to leave only numbers and spaces
Robert Poole

5+ Year Member



 
Msg#: 3907758 posted 11:11 am on May 6, 2009 (gmt 0)

Hey campers.

Let's say I wish to take an input field which is a phone number, and strip out everything that is not a number or a space. For instance...

(01234) 567890 would become 01234 567980 (not 01234567890).

I just don't know how to configure the POSIX part of the ereg_replace statement. I've tried about a bazillion approaches but have come up empty.

Should be a quite simple one for you big-brains out there!

Cheers

Roberto

 

dreamcatcher

WebmasterWorld Senior Member dreamcatcher us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3907758 posted 3:01 pm on May 6, 2009 (gmt 0)

You could use str_replace [php.net].

$phone = str_replace(array('(',')',array(),$phone);

dc

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3907758 posted 4:03 pm on May 6, 2009 (gmt 0)

$phone = preg_replace('/[^\d\s]+/','',$phone);

\d = digit
\s = space
^ not operator, when the first character inside a character class
[] character class
+ one or more

Summary: replace one or more instances of anything not a digit or space in the string "$phone" with an empty string.

Robert Poole

5+ Year Member



 
Msg#: 3907758 posted 9:49 am on May 7, 2009 (gmt 0)

Like it guys!

Thanks for your speedy and effecient responses, and an extra thanks to rocknbil for the explanation to go with!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved