Just try with:
$input = '604-555-5555';
if (preg_match('/^\d{10,13}$/', str_replace('-', '', $input))) {
// valid
}
سؤال
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.
This means the following numbers are correct
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
}
?>