Re: phone number formatting

From: Date: Thu, 02 Nov 2000 01:45:53 +0000
Subject: Re: phone number formatting
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-23294@lists.php.net to get a copy of this message
Hi Jerry- I am learning regular expressions so will tackle this one, keep in mind I am learning. I use PRCE because that is what I am learning :-) Created a little function here, you can snip whatever out of course but wanted to add a little sanity checking, it can use more in future as to take in account non-US folk but anyway here goes : <?php // Purpose : Convert 0123456789 into (012) 345-6789 // Use : $value = '4115551212'; // print PhoneNumberFormat($value); function PhoneNumberFormat ($value) { // replace non-numbers with nothing (strip them) jic $value = preg_replace('/[^0-9]/','',$value); // get length of value $valuelength = strlen($value); if ($valuelength == '10') { $value = preg_replace('/^(\d{3})(\d{3})(\d{4})/','(\\1) \\2-\\3',$value); Return $value; // like this (411) 555-1212 } else { Return $value; // If not 10 then return it // Or return something else I suppose } } ?> The key to the output is : '(\\1) \\2-\\3' it should look familiar to the actual output right? The ( ) and - are in there. This is a good start and does what you need. It might be fun to add checks for other phone numbers but I am not at all familiar with global phone number schemes - all thoughts welcome as I desire to have global phone number check (we are part of global economy now !!!) Regards, Philip -- be outside the box. On Wed, 1 Nov 2000, Jerry Lake wrote: > If I have phone numbers stored in my database > as "1234567890" when I call them out to a page > how can I display them as "(123) 456-7890" > some sort of ereg_replace(); I'm guessing, but not sure how to structure. > any help is appreciated > Regards, > > J. > > > -- > 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 (#23294) next »