Formatting Phone Numbers in PHP

Tags: ,

I am working on an SMS app and need to be able to convert the sender’s phone number from +11234567890 to 123-456-7890 so it can be compared to records in a MySQL database.

The numbers are stored in the latter format for use elsewhere on the site and I would rather not change that format as it would require modifying a lot of code.

How would I go about this with PHP?



$data = '+11234567890';

if(  preg_match( '/^+d(d{3})(d{3})(d{4})$/', $data,  $matches ) )
    $result = $matches[1] . '-' .$matches[2] . '-' . $matches[3];
    return $result;

Source: stackoverflow