What to do with Phone Numbers?

September 25th, 2007 um 2:10 Uhr • PHP, asd String Manipulation0 Kommentare

You’re asked to develop a form for a registration page of some sort. You’re database table has one column for a variety of phone numbers. You have to display hundreds if not thousands of these registrations numbers in company-wide reports. How do you ensure that the phone numbers that hundreds if not thousands of registrants enter are uniform. I have a pair of PHP functions that I use.

 


function formatPhoneNumber($phoneNumber)
{
$phoneNumber = stripPhoneNumber($phoneNumber);
$p1 = substr($phoneNumber,0,3);
$p2 = substr($phoneNumber,3,3);
$p3 = substr($phoneNumber,6,4);

$fPhone = $p1 . "-" . $p2 . "-" . $p3;
if($fPhone == "–") $fPhone = "";
return $fPhone;
}

function stripPhoneNumber($phoneNumber)
{
$cleanPhone = preg_replace("/[^0-9]/","",$phoneNumber);

return $cleanPhone;
}


 

stripPhoneNumber() removes all non-numeric characters while formatPhoneNumber() creates a uniform presentation of the phone number to end user. I use stripPhoneNumber() to store the value in a varchar(10) field in a mySQL database table. In my validation page I check to make sure that there are not more than 10 characters at least. When I pull the data out of the database to present it to an executive or manager of some kind I use the formatPhoneNumber() which in this case displays the phone number in a ###-###-#### format.

The limitation of course is that phone numbers must be in a ten digit largely American format.  However, I think you can see that these functions can be easily adapted for a variety of possibilities worldwide.

 

Bis jetzt keine Kommentare

RSS feed for comments on this post. TrackBack URI


Leave a comment

XHTML: Du kannst folgende Tags verwenden:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>