A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearNeed to re-format phone number entries in a PHP Formmail script - page 1
User InfoPosts
Need to re-format phone number entries in a PHP Formmail script#1
I have a simple text field for "Phone Number" in a contact form on a client's website. The formmail script returns whatever the user types into the field. For example, they'll receive "000-000-0000", "0000000000", (000) 000-000, etc. The client would like to receive all phone numbers in this form: 000-000-0000. Can someone provide a simple script that would strip out all extraneous punctuation, then re-insert the dashes?

I'm not a programmer, just a designer so I can't provide any existing code for anyone to evaluate, though I'll be happy to email the formmail script to anyone who can help.

Thanks.
A. Grant

posted date: 2008-12-20 13:47:00


Re: Need to re-format phone number entries in a PHP Formmail script#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-20 13:47:01


Re: Need to re-format phone number entries in a PHP Formmail script#3
something like thisfunction formatPhone($number){ $number = str_replace(array('(', ')', '-', ' '), '', $number); if (strlen($number) == 10) { $area = substr($number, 0, 3); $part1 = substr($number, 3, 3); $part2 = substr($number, 6); return "$area-$part1-$part2"; } else { return false; }}If the number passed in is 10 digits long, it will return the properly formatted number. Otherwise, it will return FALSE

posted date: 2008-12-20 14:01:00


Re: Need to re-format phone number entries in a PHP Formmail script#4
<?phpfunction formatPhone($number) { $number = preg_replace('/[^\d]/', '', $number); //Remove anything that is not a number if(strlen($number) < 10) { return false; } return substr($number, 0, 3) . '-' . substr($number, 3, 3) . '-' . substr($number, 6); }foreach(array('(858)5551212', '(858)555-1212', '8585551212','858-555-1212', '123') as $number) { $number = formatPhone($number); if($number) { echo $number . "\n"; } } ?>the above returns:858-555-1212858-555-1212858-555-1212858-555-1212

posted date: 2008-12-20 14:38:00


Re: Need to re-format phone number entries in a PHP Formmail script#5
Note: TenebrousX's answer is more or less the same, but if we were playing golf I won.

posted date: 2008-12-20 14:40:00


Re: Need to re-format phone number entries in a PHP Formmail script#6
function format_phone($phone){ $phone = preg_replace("/[^0-9]/", "", $phone); if(strlen($phone) == 7) return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone); elseif(strlen($phone) == 10) return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone); else return $phone;}

posted date: 2011-08-15 12:42:00


Re: Need to re-format phone number entries in a PHP Formmail script#7
//phone number format//example $phone = '1111111111'$area = substr($phone, 0, 3);$part1 = substr($phone, 3, 3);$part2 = substr($phone, 6);$phone = '('.$area.') '.$part1.'-'.$part2;echo $phone;//will look like (111) 111-1111

posted date: 2012-01-16 08:46:00


Re: Need to re-format phone number entries in a PHP Formmail script#8
This won't do what the questioner asked to do. Note that the input may vary (i.e. is not a simple "number"-string) and can not be parsed with fixed substrings. You'd have to "sanitize" input first.

posted date: 2012-01-16 09:00:00


Re: Need to re-format phone number entries in a PHP Formmail script#9
Someone needs to make this the correct answer...

posted date: 2012-10-18 05:11:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.