栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用php / regex验证美国电话号码

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用php / regex验证美国电话号码

您可以使用先行断言来解决此问题。基本上,我们要说的是我想要一系列特定的字母(例如,ex,ext,x,extension),然后是一个或多个数字。但我们也想涵盖根本没有扩展的情况。

旁注,您不需要在诸如[ s]或其后的[x]之类的单个字符周围加括号。另外,您可以将原本位于同一位置的字符分组,因此可以使用[ s ./]代替
s?。?/?。意思是“这些字符中的任何一个”

这是regex的更新,也可以在这里解决您的评论。我已经在实际代码中添加了解释。

<?php    $sPattern = "/^        (?:# Area Code (?:(    # Open Parentheses     (?=d{3}))      # Lookahead.  only if we have 3 digits and a closing parentheses )? (d{3})   # 3 Digit area pre (?:     (?<=(d{3})     # Closing Parentheses.  Lookbehind.     )    # only if we have an open parentheses and 3 digits )? [s./-]? # Optional Space Delimeter        )?        (d{3})       # 3 Digits        [s./-]?    # Optional Space Delimeter        (d{4})s?    # 4 Digits and an Optional following Space        (?:# Extension (?:       # Lets look for some variation of 'extension'     (?:         (?:e|x|ex|ext).?       # First, abbreviations, with an optional following period     |         extension    # Now just the whole word     )     s?   # Optionsal Following Space ) (?=d+)   # This is the Lookahead.  only accept that previous section IF it's followed by some digits. (d+)     # Now grab the actual digits (the lookahead doesn't grab them)        )? # The Extension is Optional        $/x";         // /x modifier allows the expanded and commented regex    $aNumbers = array(        '123-456-7890x123',        '123.456.7890x123',        '123 456 7890 x123',        '(123) 456-7890 x123',        '123.456.7890x.123',        '123.456.7890 ext. 123',        '123.456.7890 extension 123456',        '123 456 7890',         '123-456-7890ex123',        '123.456.7890 ex123',        '123 456 7890 ext123',        '456-7890',        '456 7890',        '456 7890 x123',        '1234567890',        '() 456 7890'    );    foreach($aNumbers as $sNumber) {        if (preg_match($sPattern, $sNumber, $aMatches)) { echo 'Matched ' . $sNumber . "n"; print_r($aMatches);        } else { echo 'Failed ' . $sNumber . "n";        }    }?>

和输出:

Matched 123-456-7890x123Array(    [0] => 123-456-7890x123    [1] => 123    [2] => 456    [3] => 7890    [4] => 123)Matched 123.456.7890x123Array(    [0] => 123.456.7890x123    [1] => 123    [2] => 456    [3] => 7890    [4] => 123)Matched 123 456 7890 x123Array(    [0] => 123 456 7890 x123    [1] => 123    [2] => 456    [3] => 7890    [4] => 123)Matched (123) 456-7890 x123Array(    [0] => (123) 456-7890 x123    [1] => 123    [2] => 456    [3] => 7890    [4] => 123)Matched 123.456.7890x.123Array(    [0] => 123.456.7890x.123    [1] => 123    [2] => 456    [3] => 7890    [4] => 123)Matched 123.456.7890 ext. 123Array(    [0] => 123.456.7890 ext. 123    [1] => 123    [2] => 456    [3] => 7890    [4] => 123)Matched 123.456.7890 extension 123456Array(    [0] => 123.456.7890 extension 123456    [1] => 123    [2] => 456    [3] => 7890    [4] => 123456)Matched 123 456 7890Array(    [0] => 123 456 7890    [1] => 123    [2] => 456    [3] => 7890)Matched 123-456-7890ex123Array(    [0] => 123-456-7890ex123    [1] => 123    [2] => 456    [3] => 7890    [4] => 123)Matched 123.456.7890 ex123Array(    [0] => 123.456.7890 ex123    [1] => 123    [2] => 456    [3] => 7890    [4] => 123)Matched 123 456 7890 ext123Array(    [0] => 123 456 7890 ext123    [1] => 123    [2] => 456    [3] => 7890    [4] => 123)Matched 456-7890Array(    [0] => 456-7890    [1] =>     [2] => 456    [3] => 7890)Matched 456 7890Array(    [0] => 456 7890    [1] =>     [2] => 456    [3] => 7890)Matched 456 7890 x123Array(    [0] => 456 7890 x123    [1] =>     [2] => 456    [3] => 7890    [4] => 123)Matched 1234567890Array(    [0] => 1234567890    [1] => 123    [2] => 456    [3] => 7890)Failed () 456 7890


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470803.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号