简体   繁体   English

10-13 位(国际)电话号码的正则表达式

[英]Regex for 10-13 digit (international) phone numbers

I need to deliver (international) phone numbers from a PHP registration form to an external party.我需要将(国际)电话号码从 PHP 注册表发送到外部方。 The external party has the following rules for phone numbers.外部方对电话号码有以下规则。

  • 10-13 digits 10-13位
  • dashes are allowed破折号是允许的

This means the following numbers are correct这意味着以下数字是正确的

  • 0046701234567 (Swedish) 0046701234567 (瑞典语)
  • 604-555-5555 604-555-5555
  • 5675555555 5675555555

What would be the best (and most correct) regex to regulate and check all the phone numbers so I can store them in a global database to send them to the external party afterwards?什么是最好的(也是最正确的)正则表达式来规范和检查所有电话号码,以便我可以将它们存储在全局数据库中,然后将它们发送给外部方?

Just try with:只需尝试:

$input = '604-555-5555';

if (preg_match('/^\d{10,13}$/', str_replace('-', '', $input))) {
  // valid
}
<?php 
  $input = '604-555-5555';  
  if (preg_match('/[\d-]{10,13}+/', $input, $r) ) {
    // valid
    foreach($r as $e){echo $e;}do formatting      
  } 
?>

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

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