I want to check that an entered phone number is a valid E164-formatted phone number; if not, then convert it to proper E164 format.
Asked
Active
Viewed 2,263 times
1

mickmackusa
- 43,625
- 12
- 83
- 136

Sonali Hajarnis
- 187
- 7
-
We have try following link code https://stackoverflow.com/questions/4708248/formatting-phone-numbers-in-php – Sonali Hajarnis Jun 22 '17 at 13:05
-
I expect that you should be able to customize this comprehensive script to suit your needs: https://stackoverflow.com/a/69551677/2943403 – mickmackusa Jul 16 '22 at 13:09
-
Or maybe [here](https://stackoverflow.com/a/58725560/2943403) on the same page. – mickmackusa Jul 16 '22 at 13:18
1 Answers
-1
Use Regular Expressions in PHP.
The regex I had to come up with that works along with '+' sign along with the digits which much reside between 10~15 is as follows:
^+?[1-9]\d{1,14}$
And this works well. You can check it out on Regex101.

Shayan Ahmad
- 952
- 8
- 17
-
Why is the `+` allowed to occur unlimited times? Why are you engaging a lazy quantifier when the regex engine will be matching plus symbols until it encounters a digit. Why use the verbose `[0-9]` then immediately use the shorter +\d`? Why not just adjust the quantifier range and write a DRYer pattern? I don't find this pattern to be very thoughtful/refined. – mickmackusa Jul 16 '22 at 13:12
-
Hi @mickmackusa, I believe you are right. This was from four years ago when I solved a similar situation. Maybe the situation here for this question was quite different. Now I understand things a bit differently. Based upon that I have updated the regular expression. Could you check now if it's better? – Shayan Ahmad Jul 16 '22 at 13:55