Regex which controls phone numbers

Refresh

6 days ago

Views

1.3k time

1

i'm new to regex, and i only need one statement.

I want that my statement accepts these numbertyps:

06643823423 could be longer or 0664 3843455 or +43664 4356999

and it's important that these is only one statement.

can anyone help me?

4 answers

0

out ofwhat you have there, the only regex I could come up with is:

$phone_number = '+449287645367';
var_dump(preg_match("/^[\+|0][0-9]{10,12}$/",str_replace(' ','',$phone_number)));
1

How about:

^\+?[0-9 ]+$

You can use that with preg_match, e.g.

$matches = preg_match("/^\+?[0-9 ]+$/", $telephone_number);
0

Try \+?\d+\s?\d+

To explain:
\+? - a plus sign (escaped with \, since '+' is a special character in regex). The '?' means '0 or 1 of the preceding characters' making it optional
\d+ - \d means a digit; the plus sign means 1 or more
\s? - \s means a white space character; the ? makes it optional
\d+ - \d means a digit; the plus sign means 1 or more

So this should match 2 or more digits, with an optional + sign at the beginning, and an optional space somewhere in the middle.

0
$regExp = '/^([+][4][3]|[0]){1}([0-9]{3})([ ]{0,1})([0-9]{7})$/';
$number = "06643823423";

if(!preg_match($regExp,trim($number)))
{
        echo FALSE;
}
else
{                
        echo TRUE;
}