检查字符串是否全是数字且长度为10个字符


Check if a string is all numbers and 10 chars long

只是在寻找一种快速的方法使用PHP来检查字符串是否全是数字和10个数字长

,

4343434345 -这将被接受(10个字符全数字)

343434344 -这不会(9个字符)

sdfefsefsdf -这不会。

谢谢

if (preg_match('~^'d{10}$~', $str)) { ... }

if (ctype_digit($str) && strlen($str) == 10) { ... }

ex:-

if(preg_match('/^'d{10}$/', $str))
    echo "A match was found.";
} else {
    echo "A match was not found.";
}