[英]php: echo an error message
code : 代码:
$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;
In this code, is there a way to identify $searchText have only 4 digits numbers or it contain string etc. If it's so, I want to echo an error message. 在这段代码中,有没有办法识别$ searchText只有4位数字或它包含字符串等。如果是这样,我想回显一条错误信息。
Thanks for any comments. 谢谢你的评论。
Check if $code is a text using is_int
and the length of each number using strlen();
使用strlen();
检查$ code是否是使用is_int
的文本和每个数字的长度strlen();
in an if statement: 在if语句中:
if (is_int($code)) {
if (strlen($num1) = 4 && strlen($num2) ...) {
//code here
}
else
echo "Error.";
}
else
echo "The search is text";
This will work to verify that they are digits only and 4 characters only... 这将验证它们只是数字而且只有4个字符...
//is not digits or //is not 4 characters in length
if ( (preg_match("/[^0-9]/i"), $code) || (strlen($code) != 4) ) {alert error...}
GOT IT! 得到它了!
<?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';
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.