سؤال

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      
  } 
?>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top