简体   繁体   中英

Regex for 10-13 digit (international) phone numbers

I need to deliver (international) phone numbers from a PHP registration form to an external party. The external party has the following rules for phone numbers.

  • 10-13 digits
  • dashes are allowed

This means the following numbers are correct

  • 0046701234567 (Swedish)
  • 604-555-5555
  • 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      
  } 
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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