0

这个问题很简单。我不太清楚regular expressions. 这可以使用正则表达式来完成吗?

如果用户输入两位或三位数字,则回显This number cant be used。如果用户输入任何其他数字,则回显Go ahead

4

5 回答 5

4

不需要正则表达式。

if (!ctype_digit($target) || strlen($target) == 2 || strlen($target) == 3) {
    # Number is invalid
}
于 2013-06-11T05:54:56.237 回答
0

try this

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

this will work for both conditions

$string = 111;
$string = "111";
于 2013-06-11T06:07:52.237 回答
0
if (!is_numeric($str) || strlen($str) == 2 || strlen($str) == 3) 
{
    echo "This number cant be used";
}
else
{
    echo "Go dhead";
}
于 2013-06-11T05:58:58.507 回答
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'; 
}
于 2013-06-11T06:00:34.693 回答
0

简单的一个班轮...

echo strlen($yournumber)==2 || strlen($yournumber)==3 ? "Number cant be used" : "Go ahead";
于 2013-06-11T05:55:21.283 回答