简体   繁体   English

正则表达式从字符串中查找带有国家代码的电话号码

[英]Regex to find phone number with country code from string

I currently have the following:我目前有以下内容:

    preg_match_all('/\b[0-9]{3}\s*-\s*[0-9]{3}\s*-\s*[0-9]{4}\b/',$cacax,$matches);
    echo '<pre>';
    print_r($matches[0]);

This will output:这将 output:

[0] => 888-618-0367

instead of代替

 [0] => +1 888-618-0367

I got no clue on how to make that regex expression only match +1 starting numbers and also include the number in the match.我不知道如何使该正则表达式仅匹配 +1 起始数字并且还包括匹配中的数字。

Will appreciate any help, thanks!将不胜感激任何帮助,谢谢!

Add \+1\s* at the start, use在开头添加\+1\s* ,使用

\+1\s*[0-9]{3}\s*-\s*[0-9]{3}\s*-\s*[0-9]{4}\b

See proof .证明 The \+1\s* part will require +1 and zero or more whitespace characters at the start of your matches. \+1\s*部分在匹配开始时需要+1和零个或多个空格字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM