Format Phone Number using PHP

/ By Arjun / Published on October 10, 2014

Below is a tiny function that will take any 10 digit number and convert it into the following phone format: 970-313-2428.

That’s it.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

5 1 vote
Article Rating
Subscribe
Notify of
guest
7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Pradeep Kushwaha
Pradeep Kushwaha
5 years ago

Hii
i try to coonvert html to m pdf . In html part i am using bootstrap css which is not apply to the pdf .So help me out that how to add external css to the pdf
thanks

badgermouse
badgermouse
3 years ago

The $numbers_only variable should be $numbers. Otherwise, nothing is returned, since that variable hasn’t been defined.

arjun
3 years ago
Reply to  badgermouse

Correct. Thank you. I will update it

Mpay Recharge
Mpay Recharge
3 years ago

hi sir pls help me how to mobile lookup with operator code for php my mail id [email protected]

arjun
3 years ago
Reply to  Mpay Recharge

I did not get you. Can you describe your problem?

Waqas Ahmed (Hobbytronics Paki
Waqas Ahmed (Hobbytronics Paki
2 years ago

Hello,

**Background:**

I am sending sms alerts to my wordpress website users using the code in my functions.php child theme file and it is working correctly.
My sms api only supports the phone number in the format of 923XXXXXXXXX (12 digits phone number) where 92 is my country code and 3 mentions that the number belongs to a cellular network.

**Problems:**

**Problem A:**

Different users provide their phone number in the following different formats;

– 03XXXXXXXXX
– 3XXXXXXXXX
– +923XXXXXXXXX
– 923XXXXXXXXX

Only the fourth format is correct one. I want a code which alters rest of three format styles into the fourth one by;

– removing first digit of “0” from the number and add 92 in 1st example
– add “92” in 2nd example
– remove “+” sign in 3rd example

**Problem B:**
If a number is less than or greater than 12 digits, it should terminate the code without proceeding further.

May someone please help me to achieve this task?

Steve Fritz
Steve Fritz
6 months ago

Thank you!

7
0
Would love your thoughts, please comment.x
()
x