I need to write a regex that will match only this formats
+420 000 000 000
+420000000000
420 000 000 000
420000000000
It can't match any az character in any part of string, just numbers, white space and "+" on the beginning.
You could try the below regex,
^\+?\d{3} ?\d{3} ?\d{3} ?\d{3}$
^
Asserts that we are at the start. \\+?
optional +
\\d{3}
Matches exactly three digits. <space>?
optional space $
Asserts that we are at the end. This regex should work for you:
$mobileNumber = "0905 222 222";
if ( preg_match("/^(\+?)([0-9] ?){9,20}$/", $mobileNumber) )
echo "matches!";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.