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?
How about:
^\+?[0-9 ]+$
You can use that with preg_match, e.g.
$matches = preg_match("/^\+?[0-9 ]+$/", $telephone_number);
$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;
}
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.
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)));
精彩评论