📄 11c01-1.php
字号:
<?php// A function that will accept US phone numbers, in most every format// and standardize them to xxx-xxx-xxxx format.function standardize_phone($phone) { // First, remove all non-digits from the string $p = preg_replace('/[^0-9]/', '', $phone); // Now, break it into it's appropriate parts and insert dashes. return substr($p, 0, 3) . '-' . substr($p, 3, 3) . '-' . substr($p, 6);}// A function to check for phone number validity// It requires a standardized numberfunction validate_phone($phone) { // First split the number into 3 parts: $parts = explode('-', $phone); // If the middle is '555' if ($parts[1] == '555') { // Invalid if the final part is between 0100 and 0199 if (($parts[2] >= 100) && ($parts[2] < 200)) { return false; } } // Invalid if the first digit of the area code is 0 or 1 if ($parts[0] < 200) { return false; } // Invalid if the second digit of the area code, is 9 if ($parts[0]{1} == '9') { return false; } // Check that the last number has 4 characters: if (strlen($parts[2]) != 4) { return false; } // Otherwise, we made it, it's valid. return true;}// Standardize & validate some phone numbers:$phones = array('(108)355-4688', '354-555-0103', '294.423.8437', '301 867-5309', '424-726 739', '829-56628426', 'Hello');foreach ($phones as $num) { $st = standardize_phone($num); $valid = validate_phone($st); $output = $valid ? 'Valid' : 'Invalid'; echo "<p>{$st} - {$output}</p>\n";}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -