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

$code がテキストであるかどうか、および if ステートメントでis_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

とった!

 <?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 に答える
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 に答える