为什么 ereg( "^d{11}$" ,18311111111) 是假的?



我的代码是:

<?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
}

最新更新