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
}
?>