February 2008
Intermediate to advanced
216 pages
4h 33m
English
As with email addresses, there's no way to make sure a telephone number is valid outside of making a real telephone call. However, you can validate the number of digits and put it into standard format. The following function returns a pure 10-digit phone number if the number given is 10 digits or 11 digits starting with 1. If the number does not conform, the return value is false.
<?php
function is_phone($phone) {
$phone = preg_replace('/[^\d]+/', '', $phone);
$num_digits = strlen($phone);
if ($num_digits == 11 && $phone[0] == "1") {
return substr($phone, 1);
} else if ($num_digits == 10) {
return $phone;
} else {
return false;
}
}
?>This script shows the power of regular expressions combined with standard string ...
Read now
Unlock full access