Asked  7 Months ago    Answers:  5   Viewed   28 times

What is the best way to add dashes to a phone number in PHP? I have a number in the format xxxxxxxxxx and I want it to be in the format xxx-xxx-xxxx. This only applies to 10 digit US phone numbers.


$number = "1234567890";
$formatted_number = preg_replace("/^(d{3})(d{3})(d{4})$/", "$1-$2-$3", $number);

EDIT: To be a bit more generic and normalize a US phone number given in any of a variety of formats (which should be common practice - there's no reason to force people to type in a phone number in a specific format, since all you're interested in are the digits and you can simply discard the rest):

function localize_us_number($phone) {
  $numbers_only = preg_replace("/[^d]/", "", $phone);
  return preg_replace("/^1?(d{3})(d{3})(d{4})$/", "$1-$2-$3", $numbers_only);

echo localize_us_number("5551234567"), "n";
echo localize_us_number("15551234567"), "n";
echo localize_us_number("+15551234567"), "n";
echo localize_us_number("(555) 123-4567"), "n";
echo localize_us_number("+1 (555) 123-4567"), "n";
echo localize_us_number("Phone: 555 1234567 or something"), "n";
Wednesday, March 31, 2021
answered 7 Months ago

Both array_push and the method you described will work.

$cart = array();
$cart[] = 13;
$cart[] = 14;
// etc

//Above is correct. but below one is for further understanding
$cart = array();
    $cart[] = $i;  
echo "<pre>";
echo "</pre>";

Is the same as:

$cart = array();
array_push($cart, 13);
array_push($cart, 14);

// Or 
$cart = array();
array_push($cart, 13, 14);
Wednesday, March 31, 2021
answered 7 Months ago

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario:

Related usability ticket:

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
answered 7 Months ago

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
answered 5 Months ago

Earlier this year, I reviewed a bunch of ruby gems that parse and format phone numbers. They fall into a number of groups (see below). TLDR: I used 'phone'. It might work for you because you can specify a default country code that it uses if your phone number doesn't include one.

1) US-centric:

big-phoney (0.1.4)
phone_wrangler (0.1.3)
simple_phone_number (0.1.9)

2) depends on rails or active_record:

phone_number (1.2.0)
validates_and_formats_phones (0.0.7)

3) forks of 'phone' that have been merged back into the trunk:

elskwid-phone (
tfe-phone (

4) relies on you to know the region ahead of time

phoney (0.1.0)

5) Kind of almost works for me

phone (

6) does not contain the substring 'phone' in the gem name (edit: I see you tried this one)

phony (1.6.1)

These groupings may be somewhat unfair or out of date so feel free to comment. I must admit I was a little frustrated at the time at how many people had partially re-invented this particular wheel.

Thursday, August 12, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :