mikepenz
mikepenz

Reputation: 12868

Regex which controls phone numbers

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?

Upvotes: 1

Views: 1361

Answers (4)

RobertPitt
RobertPitt

Reputation: 57268

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)));

Upvotes: 0

Mike C
Mike C

Reputation: 3117

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.

Upvotes: 0

Maulik Vora
Maulik Vora

Reputation: 2584

$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;
}

Upvotes: 0

Bobby Jack
Bobby Jack

Reputation: 16048

How about:

^\+?[0-9 ]+$

You can use that with preg_match, e.g.

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

Upvotes: 1

Related Questions