PHP regular expressions (phonenumber)

I am having problems with regular expression for phone numbers. I am trying to create a regular expression that is as high as possible for European phone numbers. A phone number can begin with a + or two leading 0 characters, followed by a number from 0 to 40. This is optional, so this first part can also be ignored. After that, everyone should be numbers grouped in pairs of at least two, with a space or a-inbetween groups.

The regular expression that I have compiled can be found below.

/((\+|00)+[0-4]+[0-9]+)?([ -]?[0-9]{2,15}){1,5}/ 

This should be consistent with the following structures

 0031 34-56-78 0032123456789 0033 123 456 789 0034-123-456-789 +35 34-56-78 +36123456789 +37 123 456 789 +38-123-456-789 ... 

That it also matches according to my javascript

 +32 a54b 67-0: 

So I must have made a mistake, but I really can't see it. Any help would be greatly appreciated.

+6
source share
3 answers

The problem is that you are not using ^ $ bindings to determine the start and end of a line and therefore find a match anywhere in the line.

 /^((\+|00)+[0-4]+[0-9]+)?([ -]?[0-9]{2,15}){1,5}$/ 

Adding anchors will do the trick. More information about these metacharacters can be found here .

+10
source

Put ^ at the beginning of RegExp and $ at the end.

+1
source

Try it, maybe it can help you.

 if (ereg("^((\([0-9]{3}\) ?)|([0-9]{3}-))?[0-9]{3}-[0-9]{4}$",$var)) { $valid = true; } 
+1
source

Source: https://habr.com/ru/post/899955/


All Articles