我需要一个
PHP函数来验证字符串,所以它只能在前面包含数字和加号().
例:
632444747将返回true
632444747将返回true
632444747将返回false
& 632444747将返回false
如何使用正则表达式实现这一目标?
谢谢.
像这样的东西
preg_match('/^\+?\d+$/',$str);
测试它
$strs = array('+632444747','632444747','632444747+','&632444747'); foreach ($strs as $str) { if (preg_match('/^\+?\d+$/',$str)) { print "$str is a phone number\n"; } else { print "$str is not a phone number\n"; } }
产量
+632444747 is a phone number 632444747 is a phone number 632444747+ is not a phone number &632444747 is not a phone number