字符串中没有空格,并且含有0-9的数字。 如何判断?

解决方案 »

  1.   

    if(preg_match('/\d+/',$str)&&!preg_match('/\\s/',$str)){}
      

  2.   

    $string = "sdgfd 1234";
    $test = preg_match("/([0-9]+)/i",$string);
    $test2 = preg_match("/\\s/i",$string);
    if($test && !$test2) {
    echo "success!";
    } else {
    echo "failed!";
    }
      

  3.   

    如果是除了数字还有其他字符的可以使用以下正则:
    /^\S*\d+\S*$/其中\S代表非空字符串如果你只允许含有数字的话,可以使用下面的正则:
    /^\d+$/