Re: Number formatting

From: Date: Mon, 08 Jan 2001 20:29:10 +0000
Subject: Re: Number formatting
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-33303@lists.php.net to get a copy of this message
Here is a a function I started a couple months ago yet never finished, currently it takes 7,10,11 digit numbers and formats them according to need else it is dumb and unsure what to do so returns input. The key to output format is in "mixed replacement" part of preg_replace() (see manual) . I've commented the function to hopefully make sense to you. This assumes everyone is from around North America and like I said, is incomplete in terms of global usefulness. I think the regex could be simplifed (one vs three+) but am a relative regex novice. Here you go. Run this (or something like it) before putting into db. : <?php // Examples of use $num10 = '253-333-4444'; $num11 = '1(253)---333-4444'; $num7 = '3334444'; print phoneFormat($num10); // prints (253) 333-4444 print phoneFormat($num11); // prints (253) 333-4444 print phoneFormat($num7); // prints 333-4444 function phoneFormat ($value) { // replace non-numbers with nothing (strip away anything other then 0-9) // this is the key to what you want to do. $value = preg_replace('/[^0-9]/','',$value); // get length of value $valuelength = strlen($value); // if it is 10 numbers then assume it is good and format accordingly if ($valuelength == 10) { $value = preg_replace('/^(\d{3})(\d{3})(\d{4})/', '(\\1) \\2-\\3',$value); Return $value; } // if 11 numbers and begins with 1 then 'hide' the one and format elseif ($valuelength == 11 && substr($value,-11,1) == 1) { $value = preg_replace('/^(\d{1})(\d{3})(\d{3})(\d{4})/', '(\\2) \\3-\\4',$value); Return $value; // if 7 digits then return without area code but still format } elseif ($valuelength == 7) { $value = preg_replace('/^(\d{3})(\d{4})/', '\\1-\\2',$value); Return $value; // else play dumb and return value } else { Return $value; } } // EOF; ?> Regards, Philip Olson http://www.cornado.com/ On Mon, 8 Jan 2001, Ashley M. Kirchner wrote: > > I have a standard <form> page that passes the information to a PHP > script which in turn feeds the data to an mySQL database. What I'd like > to do is have some sort of consistency in some of the fields, especially > the phone number field. > > As people fill in their data, I'm finding out that putting a little > command on the <form> page (stating how to enter the phone number) gets > completely ignored, and I get stuff like this in the DB: > > 123-555-1212 > 123.555.1212 > (123) 555-1212 > (123) 555 1212 > 123) 555 1212 > 1235551212 > (123)555-1212 > 123 555 1212 > 123/555/1212 > > ...some add extra characters behind it, like 'ext' (for extension) > and 'or' (for listing a second number). However, the field in the > <form> page is just long enough for 15 characters, so they'll never be > able to enter anything more. > > Is there an easy way to convert this gibberish data into a standard > format prior to feeding it to the DB, say (123) 555-1212 or whatever > other format I want to use? > > AMK4 > > -- > H | Hi, I'm currently out of my mind. Please leave a message. BEEEEP! > |____________________________________________________________________ > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > Ashley M. Kirchner <mailto:ashley@pcraft.com> . > 303.442.6410 x130 > Director of Internet Operations / SysAdmin . 800.441.3873 x130 > Photo Craft Laboratories, Inc. . eFax 248.671.0909 > http://www.pcraft.com . 3550 Arapahoe > Ave, #6 > .................. . . . . Boulder, CO 80303, U.S.A. > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#33303) next »