Home > Featured > Remove all characters except numbers from a phone number and format by adding dashes in PHP
Bookmark and Share

Remove all characters except numbers from a phone number and format by adding dashes in PHP

Posted on October 29, 2012 | Filed in Featured, PHP, The Web

<?php

$number = “(212)222.1111”;

//Strip Out Everything Except Numbers
$formatted_number=ereg_replace(“[^0-9]”, “”, $number);

//Add Dashes
$formatted_number = substr ($formatted_number, 0, 3) . ‘-‘ . substr ($formatted_number, 3, 3) . ‘-‘ . substr ($formatted_number, 6);

echo $formatted_number;

?>

Output: 212-222-1111

Leave a Reply