Bluehost.com Web Hosting $6.95

Re: [PHP] Reformatting phone number string from user input

This is a discussion on Re: [PHP] Reformatting phone number string from user input within the PHP General forums, part of the PHP Programming Forums category; Thanks John, That does look a lot tidier. I tried a similar approach early on, but was trying to specifically ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-10-2003
Verdon Vaillancourt
 
Posts: n/a
Default Re: [PHP] Reformatting phone number string from user input

Thanks John,

That does look a lot tidier. I tried a similar approach early on, but was
trying to specifically match '(' and ')' and was running into lots of
trouble with my syntax, in specific, properly escaping \( and \) so they
were not treated as paranthesis/operators/whatever. This is much simpler. I
like simple :)

Now I can focus on inserting a default area code, if the user does not ;)

Salut,
Verdon


On 8/10/03 2:04 PM, "John W. Holmes" <holmes072000@charter.net> wrote:

> Verdon vaillancourt wrote:
>
>> Hi :)
>>
>> I've been working on reformatting a phone number string from user input via
>> a form field. Ultimately, my goal is to format all phone numbers in the same
>> way regardless of whether a user inputs '(123) 456-7890', '123-456-7890',
>> '123.456.7890', etc. before I insert them into a db.
>>
>> I know I've got a ways to go, but so far, after trying a few things I found
>> in the manual, I'm going in this direction...
>>
>> $patterns[0] = "/\(/";
>> $patterns[1] = "/\)/";
>> $patterns[2] = "/-/";
>>
>> $replacements[0] = "";
>> $replacements[1] = "";
>> $replacements[2] = " ";
>>
>> $phone = preg_replace($patterns, $replacements, $phone);
>>
>> This will change '(123) 456-7890' to '123 456 7890' which is what I am
>> after. I'm just wondering if there is a better or more elegant way to handle
>> this before I start trying to cover all the bases?

>
> Could do something like this:
>
> $phone = preg_replace('/[^0-9]/','',$phone);
> if(strlen($phone) != 10)
> { echo "phone number is not valid!"; }
> $phone = substr($phone,0,3) . ' ' . substr($phone,3,3) . ' ' .
> substr($phone,-4);
>
> Which should cover all of the bases. :)


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 02:59 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0