使用php过滤掉双位和三位数字?

时间:2013-06-11 05:50:12

标签: php regex

问题很简单。 我对regular expressions并不十分清楚。可以使用正则表达式来完成吗?

如果用户输入双位或三位数,请回显This number cant be used。 如果用户输入任何其他号码,请回显Go ahead

5 个答案:

答案 0 :(得分:4)

不需要正则表达式。

if (!ctype_digit($target) || strlen($target) == 2 || strlen($target) == 3) {
    # Number is invalid
}

答案 1 :(得分:0)

简单的一个班轮......

echo strlen($yournumber)==2 || strlen($yournumber)==3 ? "Number cant be used" : "Go ahead";

答案 2 :(得分:0)

if (!is_numeric($str) || strlen($str) == 2 || strlen($str) == 3) 
{
    echo "This number cant be used";
}
else
{
    echo "Go dhead";
}

答案 3 :(得分:0)

$your_input = 123;
if (preg_match('/^[0-9]{2,3}$/', $your_input)) { // check if its double or three digit number
    echo 'invalid input';
}
else {
    echo 'valid input'; 
}

答案 4 :(得分:0)

试试这个

if(preg_match('/^\d{2,3}$/',$string)){ echo "valid";}else{echo "not valid";}

这适用于两种情况

$string = 111;
$string = "111";