Jump to content

[SOLVED] Phone regex


Boo-urns

Recommended Posts

If you are sure that will be the format 100% of the time...

$number="(381) 323-4567";
$number = preg_replace('~\((\d{3})\) (\d{3})-(\d{4})~','$1 $2 $3',$number);

 

If you are not sure that will be the format 100% of the time...

 

$number="(381) 323-4567";
$number = preg_replace('~[-(). ]~','',$number);
$number = substr($number,0,3) . " " . substr($number,3,3) . " " . substr($number,-4);

Link to comment
https://forums.phpfreaks.com/topic/148268-solved-phone-regex/#findComment-778660
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.