0

代码 :

$searchText = '3423,  2453,  3245  ,  2425,  6765';     

$numbers = str_replace(", ", ",", $searchText);

    $code = explode(",", $numbers);

   if ( (preg_match("/[^0-9]/i"), $code) || (strlen($code) != 4) ) {

            $searchingError= 'Can not cantain string and more than 4 digists number!';
            echo '<script type="text/javascript">';
            echo "alert('" . $searchingError . "')";
            echo "</script>";   
    }   

    function validate ($a) {
    if (ctype_digit($a) && (strlen($a) == 4)) { 
        return "'$a'" ;
    } else {
        //$searchingError= 'Can not cantain string and more than 4 digists number!';
        //echo '<script type="text/javascript">';
        //echo "alert('" . $searchingError . "')";
        //echo "</script>";         
    }
}

$parsed = array_map( "validate",$code);

print_r($parsed);

$code = '(' . preg_replace('/\,+/', ',',implode(',', $parsed)) . ')';

echo '<br />' . $code;

在这段代码中,有没有办法识别 $searchText 只有 4 位数字或它包含字符串等。如果是这样,我想回显一条错误消息。

感谢您的任何评论。

4

3 回答 3

2

在 if 语句中检查 $code 是否是一个文本 usingis_int和每个数字的长度:strlen();

if (is_int($code)) {
    if (strlen($num1) = 4 && strlen($num2) ...) {
        //code here 
    }
    else
        echo "Error.";
}
else 
    echo "The search is text";
于 2012-11-03T04:07:32.997 回答
0

这将用于验证它们只是数字和 4 个字符......

       //is not digits                or    //is not 4 characters in length
 if ( (preg_match("/[^0-9]/i"), $code) || (strlen($code) != 4) ) {alert error...}
于 2012-11-03T04:11:23.787 回答
0

知道了!

 <?php

 $a = "4444 , 111X , 565656, 4444";

 $a = str_replace(" ", "", $a);

 $b = explode(",",$a);

 function validate($v){

if ((strlen($v)!=4)||(preg_match("/[^0-9]/i", $v))) { echo 'error<br/>'; }else{ echo 'success<br/>'; };

 }

 array_filter($b,"validate");


 echo 'complete';


 ?>
于 2012-11-03T04:52:02.753 回答