1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
|
<?php // RAY_phone_numbers.php - USING USA PHONE NUMBERS
error_reporting(E_ALL);
// A FUNCTION TO VALIDATE A PHONE NUMBER AND RETURN A NORMALIZED STRING
// MAN PAGE: http://discuss.fogcreek.com/joelonsoftware3/default.asp?cmd=show&ixPost=102667&ixReplies=15
function strtophone($phone, $format=FALSE, $dlm='-')
{
// TRANSLATE INPUT LIKE 1-800-BIG-DOGS
$phone = strtoupper($phone);
if (preg_match('/[A-Z]/', $phone))
{
$phone = str_replace('A', '2', $phone);
$phone = str_replace('B', '2', $phone);
$phone = str_replace('C', '2', $phone);
$phone = str_replace('D', '3', $phone);
$phone = str_replace('E', '3', $phone);
$phone = str_replace('F', '3', $phone);
$phone = str_replace('G', '4', $phone);
$phone = str_replace('H', '4', $phone);
$phone = str_replace('I', '4', $phone);
$phone = str_replace('J', '5', $phone);
$phone = str_replace('K', '5', $phone);
$phone = str_replace('L', '5', $phone);
$phone = str_replace('M', '6', $phone);
$phone = str_replace('N', '6', $phone);
$phone = str_replace('O', '6', $phone);
$phone = str_replace('P', '7', $phone);
$phone = str_replace('Q', '7', $phone);
$phone = str_replace('R', '7', $phone);
$phone = str_replace('S', '7', $phone);
$phone = str_replace('T', '8', $phone);
$phone = str_replace('U', '8', $phone);
$phone = str_replace('V', '8', $phone);
$phone = str_replace('W', '9', $phone);
$phone = str_replace('X', '9', $phone);
$phone = str_replace('Y', '9', $phone);
$phone = str_replace('Z', '9', $phone);
}
// DISCARD NON-NUMERIC CHARACTERS
$phone = preg_replace('/[^0-9]/', '', $phone);
// DISCARD A LEADING '1' FROM NUMBERS ENTERED LIKE 1-800-555-1212
if (substr($phone,0,1) == '1') $phone = substr($phone,1);
// IF LESS THAN TEN DIGITS, IT IS INVALID
if (strlen($phone) < 10) return FALSE;
// IF IT STARTS WITH '0' OR '1' IT IS INVALID, SECOND DIGIT CANNOT BE '9' (YET)
if (substr($phone,0,1) == '0') return FALSE;
if (substr($phone,0,1) == '1') return FALSE;
if (substr($phone,1,1) == '9') return FALSE;
// ISOLATE THE COMPONENTS OF THE PHONE NUMBER
$ac = substr($phone,0,3); // AREA
$ex = substr($phone,3,3); // EXCHANGE
$nm = substr($phone,6,4); // NUMBER
$xt = substr($phone,10); // EXTENSION
// ADD OTHER TESTS HERE AS MAY BE NEEDED - THESE ARE FOR LOCAL APPS
if ($ac == '900') return FALSE;
if ($ac == '976') return FALSE;
if ($ex == '555') return FALSE;
// IF NOT FORMATTED
if (!$format) return $phone;
// STANDARDIZE THE PRINTABLE FORMAT OF THE PHONE NUMBER LIKE 212-555-1212-1234
$formatted_phone = $ac . $dlm . $ex . $dlm . $nm;
if ($xt != '') $formatted_phone .= $dlm . $xt;
return $formatted_phone;
}
// DEMONSTRATION OF THE FUNCTION IN ACTION. IF ANYTHING IS POSTED?
if (!empty($_POST["phone"]))
{
// VALIDATE PHONE USING FUNCTION ABOVE
if (!$phone = strtophone($_POST["phone"], TRUE))
{
// FUNCTION RETURNS FALSE IF PHONE NUMBER IS UNUSABLE
echo "BOGUS: {$_POST["phone"]} \n";
}
else
{
// SHOW THE FORMATTED PHONE
echo "VALID: $phone\n";
}
}
// END PHP, PUT UP THE FORM TO TEST PHONE NUMBERS
?>
<form method="post">
ENTER A PHONE NUMBER:
<input name="phone" /><br/>
<input type="submit" />
</form>
TRY SOME OF THESE (COPY AND PASTE):
<br/>1-800-5551212
<br/>202-537-7560
<br/>123456789
<br/>703-356-5300 x2048
<br/>(212) 555-1212
<br/>1 + (212) 555-1212
<br/>1 (292) 226-7000
|