Revision: 12128
Updated Code
at March 6, 2009 16:57 by naz
Updated Code
function format_phone($phone) { $prefix = null; //Check if number has international prefix switch(true){ case substr($phone, 0, 1) == '+': $phone = substr($phone, 1); $prefix = '+'; break; case substr($phone, 0, 2) == '00': $phone = substr($phone, 2); $prefix = '+'; break; } //Strip all non numeric characters $phone = preg_replace("/[^0-9]/", '', $phone); switch(strlen($phone)){ case 6: return preg_replace("/([0-9]{2})([0-9]{2})([0-9]{2})/", "$1-$2-$3", $phone); break; case 7: return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone); break; case 10: return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone); break; case 11: return preg_replace("/([0-9]{3})([0-9]{4})([0-9]{4})/", $prefix . " ($1) $2-$3", $phone); break; case 12: return preg_replace("/([0-9]{4})([0-9]{4})([0-9]{4})/", $prefix . " ($1) $2-$3", $phone); break; default: return $phone; break; } }
Revision: 12127
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at March 3, 2009 05:24 by naz
Initial Code
function format_phone($phone) { $prefix = null; //Check if number has international prefix switch(true){ case substr($phone, 0, 1) == '+': $phone = substr($phone, 1); $prefix = '+'; break; case substr($phone, 0, 2) == '00': $phone = substr($phone, 2); $prefix = '+'; break; } //Strip all non numeric characters $phone = preg_replace("/[^0-9]/", '', $phone); switch(strlen($phone)){ case 6: return preg_replace("/([0-9]{2})([0-9]{2})([0-9]{2})/", "$1-$2-$3", $phone); case 7: return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone); break; case 10: return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone); break; case 11: return preg_replace("/([0-9]{3})([0-9]{4})([0-9]{4})/", $prefix . " ($1) $2-$3", $phone); break; case 12: return preg_replace("/([0-9]{4})([0-9]{4})([0-9]{4})/", $prefix . " ($1) $2-$3", $phone); break; default: return $phone; break; } }
Initial URL
Initial Description
This function clean up phone number and present it in styled way.
Initial Title
Phone number beutifier
Initial Tags
regex, php, phone
Initial Language
PHP