我的代码是:
<?php
$phone = 18311111111;
if(ereg("^d{11}$",$phone)){
echo "true";
} else {
echo "false";
}
?>
我有假吗?为什么?
因为ereg
不支持d
,您需要使用[0-9]
代替。
和 ereg
是不建议的,使用preg_match
,然后可以使用d
。
if(preg_match("/^d{11}$/",$phone)){
echo "true";
} else {
echo "false";
}
对于价值,您不应使用ereg
(已弃用)或preg_match
进行此类简单测试;您可以使用ctype_digit()
:
if (ctype_digit($phone)) {
// $phone consists of only digits
} else {
// non-digit characters were found in $phone
}