1

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.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136

1 Answers1

-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