PHP Formatting Phone Number – String Function

<?php
$regex = "/^(\(?\d{3}\)?)?[- .]?(\d{3})[- .]?(\d{4})$/"; $values = array("1235551234","800.555.1234", "123-555-1234" ); foreach ($values as $value) {
    $formattedValue = preg_replace($regex, "(\\1) \\2-\\3", $value);
    echo $formattedValue . "\n";
}
?>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.