North American Telephone Number Example. A regular expression
for matching a North American telephone number is shown
below. The pattern uses subexpressions to match the three main
components of the number. The code uses the preg_match
function
(see preg_match
for a description of this function).
<!--
Mark Motl
CS 4312
North American Telephone Numbers Example
-->
<?php
/* Check the protocol for this request. Redirect an HTTP request to
an HTTPS request.
Ref.: https://stackoverflow.com/questions/5106313/redirecting-from-http-to-https-with-php
*/
if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === "off")
{
$location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $location);
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>North American Telephone Numbers</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<p>
<?php
echo "Mark Motl - CS 4312 - North American Telephone Number<br /><br />\n";
// Obtain the data following the ? in the URL via which the page was
// accessed.
$argv = $_SERVER['QUERY_STRING'];
// Replace all occurrences of %20 with a space character. If URLs
// contain spaces when sent to a server, they are transformed into
// %20. The two characters following % are considered hexadecimal
// values and represent the character's ASCII code. 20 hex is equal to
// 32 decimal, the ASCII representation for a space character.
$argv = str_replace("%20"," ",$argv);
// Use the builtin function trim to remove leading and trailing
// whitespace characters.
$argv = trim($argv);
// Use the builtin function preg_split to tokenize the string $argv,
// using the space character as a delimiter. The delimiter can consist
// of a single space or multiple spaces. This function returns an
// array of the split string $argv.
$myArgvArray = preg_split("/ +/", $argv);
if (count($myArgvArray) != 1 ||
(count($myArgvArray) == 1 && strlen($myArgvArray[0]) == 0))
{
echo "This script expects one argument.<br />\n";
goto end;
}
$phoneNumber = $myArgvArray[0];
if (!is_numeric($phoneNumber))
{
echo "This script expects a numeric argument.<br />\n";
goto end;
}
if (preg_match("/^[0-9]{10}$/", $phoneNumber) === 0)
{
echo "This script expects a number with 10 digits.<br />\n";
goto end;
}
/* The format of a 10-digit phone number in North America is governed by
the North American Numbering Plan (NANP). The number format may be
summarized in the notation NPA-NXX-xxxx, where
1) NPA - Numbering Plan Area Code. Allowed ranges: [2-9] for the
first digit, and [0-9] for the second and third digits.
2) NXX - Central Office (exchange) Code. Allowed ranges: [2-9]
for the first digit, and [0-9] for both the second and third
digits (however, in geographic area codes the third digit of
the exchange cannot be "1" if the second digit is also "1").
3) xxxx - Subscriber Number. [0-9] for each of the four digits.
*/
// Build a regular expression pattern to recognize a 10-digit North
// American telephone number.
// Start the pattern with the delimiter and beginning anchor
$pattern = "/^";
// subexpression 1 matches the 3-digit area code
// |------1------|
// 1 1
$pattern .= "([2-9][0-9]{2})";
// ^area code ^
// subexpression 2 matches the 3-digit exchange code
// " 3 matches the last two digits of the exchange code
// " 4 matches the range 00 through 09 and 20 through 99
// " 5 matches the range 10 and 12 through 19
// |--------------2---------------|
// | |-----------3-----------||
// | ||-----4-----| |---5---|||
// 2 34 4 5 532
$pattern .= "([2-9](([02-9][0-9])|(1[02-9])))";
// ^exchange ^
// subexpression 6 matches the 4-digit subscriber number
// |---6----|
// 6 6
$pattern .= "([0-9]{4})";
// ^subsc # ^ ^
// End the pattern with closing anchor and the delimiter
$pattern .= "$/";
if (preg_match($pattern, $phoneNumber, $matches))
{
echo $phoneNumber, " is valid";
$areaCode = $matches[1];
$exchangeCode = $matches[2];
$subscriberNumber = $matches[6];
echo " and formatted as ($areaCode) $exchangeCode-$subscriberNumber";
}
else
{
echo $phoneNumber, " is invalid";
}
echo "<br />\n";
end:
?>
</p>
<p>
<?php
$location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$location = urlencode($location);
echo '<a href="https://validator.w3.org/nu/?doc=' . $location . '">';
?>
<img src="https://www.w3.org/QA/Tools/I_heart_validator"
alt="I heart Validator logo" height="31" width="80" />
</a>
</p>
</body>
</html>