检查一个字符串是否全部是数字和10个字符长

问题描述:

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

例如:

4343434345 - 这将被接受(10个字符的所有位数)

343434344 - 这不会(9个字符)

sdfefsefsdf - 这不会。

感谢

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

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

例如: -

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