Php 使用函数而不是多个preg_匹配检查数据格式
我试图用一个函数来检查多个变量的数据格式,准确地说是21,而不是对每个变量使用preg_匹配,因为它们应该是日期、数字、字母、字母数字,而不是空的。我在一行中使用以下函数进行多个数据检查,函数似乎返回false,他们有什么问题吗?提前谢谢你的帮助,这是代码Php 使用函数而不是多个preg_匹配检查数据格式,php,Php,我试图用一个函数来检查多个变量的数据格式,准确地说是21,而不是对每个变量使用preg_匹配,因为它们应该是日期、数字、字母、字母数字,而不是空的。我在一行中使用以下函数进行多个数据检查,函数似乎返回false,他们有什么问题吗?提前谢谢你的帮助,这是代码 <?php function alphabetic_check(){ $numargs = func_num_args(); $arg_list = func_get_args();
<?php
function alphabetic_check(){
$numargs = func_num_args();
$arg_list = func_get_args();
for($n = 0; $n < $numargs; $n++){
$x = preg_match("^[a-zA-Z\s]*$/", $arg_list[$n]);
if($x == false){
return false;
} else if($n == $numargs - 1 && $x == true){
return true;
}
}
}
function numeric_check(){
$numargs = func_num_args();
$arg_list = func_get_args();
for($n = 0; $n < $numargs; $n++){
$x = preg_match("^[0-9]*$/", $arg_list[$n]);
if($x == false){
return false;
} else if($n == $numargs - 1 && $x == true){
return true;
}
}
}
function alphanumeric_check(){
$numargs = func_num_args();
$arg_list = func_get_args();
for($n = 0; $n < $numargs; $n++){
$x = preg_match("^[a-zA-Z0-9\s]*$/", $arg_list[$n]);
if($x == false){
return false;
} else if($n == $numargs - 1 && $x == true){
return true;
}
}
}
function not_empty(){
$n = func_num_args();
$args = func_get_args();
for($i = 0; $i < $n; $i++){
$x = !empty($args[$i]);
if($x == false){
return false;
} else if($i == $n - 1 && $x == true){
return true;
}
}
}
?>
这是我正在开发的一种预订系统的代码,返回一个提示,说数据格式不正确,即使它应该超出if块中的代码
if(numeric_check($id, $cellphone, $people, $single, $double, $twin, $superior, $suite, $triple, $quadruple, $disabled, $ccn, $ccm, $ccy, $cccvc) && alphabetic_check($fistname, $lastname, $type) && not_empty($id, $user, $firstname, $lastname, $cellphone, $arrival, $departure, $people, $ccn, $ccm, $ccy, $cccvc, $type)){
echo "ok";
} else {
mysqli_close($connection);
echo "
<script type=\"text/javascript\">
window.alert('Data format wrong, retry.');
window.location.replace('control_panel.php');
</script>
";
}
if(数字支票($id,$mobile,$people,$single,$double,$twin,$superior,$suite,$triple,$fourple,$disabled,$ccn,$ccm,$ccy,$cccvc)和字母支票($fistname,$lastname,$type)&¬_empty($id,$user,$firstname,$lastname,$mobile,$arrigher,$deation,$people,$ccn,$ccm,$ccy,$cccvc{
回音“ok”;
}否则{
mysqli_close($connection);
回声“
警告('数据格式错误,请重试');
replace('control_panel.php');
";
}
这是手写代码,但我想知道如何压缩这些东西,如上所述:
if(preg_match("/^[0-9]*$/", $id) && preg_match("/^[a-zA-Z0-9\-\_]*$/", $user) && preg_match("/^[a-zA-Z\s]*$/", $firstname) && preg_match("/^[a-zA-Z\s]*$/", $lastname) && preg_match("/^[\+0-9]{10,}$/", $cellphone) && preg_match("/^[0-9\-]*$/", $arrival) && preg_match("/^[0-9\-]*$/", $departure) && preg_match("/^[0-9]*$/", $people) && $people > 0 && preg_match("/^[0-9]{16}$/", $ccn) && preg_match("/^[0-9]{1,2}$/", $ccm) && preg_match("/^[0-9]{4}$/", $ccy) && preg_match("/^[0-9]{3}$/", $cccvc) && preg_match("/^[a-z]*$/", $type) && !empty($id) && !empty($user) && !empty($firstname) && !empty($lastname) && !empty($cellphone) && !empty($arrival) && !empty($departure) && !empty($people) && !empty($ccn) && !empty($ccm) && !empty($ccy) && !empty($cccvc) && !empty($type)){
echo "ok";
} else {
mysqli_close($connection);
echo "
<script type=\"text/javascript\">
window.alert('Formato dati errato, riprova.');
window.location.replace('pannello.php');
</script>
";
}
(代码)如果(preg)和(preg)匹配(以下简称::)如果(preg)比赛(以下简称:::)如果(preg)比赛(以下简称::)如果(preg)比赛(以下简称::::)如果(preg)比赛(以下简称::)如果(preg)比赛(以下简称:)和(以下简称:)以及(以下)和(以下)preg)匹配(以下)和(以下)和(以下)比赛(以下简称:::(((“[a-zA-zA-zA-zA-zA-zA-zA-zA-Z0-Z0-Z0-Z0-Z0-Z0-Z0-Z0-Z0-Z0-Z0-Z0-Z0-Z0-Z0-Z0-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-9-,$出发)和预匹配(“/^[0-9]*$/”,$人)这两个时期的人们((($ccn)以及以及这两个时期的人们((((((($ccn)以及)和孕比赛(比赛((((((((((((((([0-9[0-9]{{{16}{{16}$/“,$ccn)和((((($人们)人们)人们>0和人们>0和人们>0和人们>0和人们)0和人们)0和0和(人们)0和(人们)0和(人们)0和0和(人们)人们)0和(人们)0和(人们)0和(人们)0和(人们)0和(人们)0和(人们)0和)0和孕)0和0和0和孕)的比赛比赛)和孕比赛比赛比赛比赛(((((((((((((&)和)和)和)和)孕)孕比赛)孕比赛)孕比赛)的比赛比赛(((((!empty($出发)&!empty($people)&&!empty($ccn)&!empty($ccm)&&!empty($ccy)&&!empty($cccvc)&!empty($type)){
回音“ok”;
}否则{
mysqli_close($connection);
回声“
窗口警报('Formato dati errato,riprova');
replace('pannello.php');
";
}
问题由Mark解决,下面是工作代码和一些测试:
<?php
function alphabetic_check(){
$numargs = func_num_args();
$arg_list = func_get_args();
for($n = 0; $n < $numargs; $n++){
$x = preg_match("/^[a-zA-Z\s]*$/", $arg_list[$n]);
if($x == false){
return false;
} else if($n == $numargs - 1){
return true;
}
}
}
function numeric_check(){
$numargs = func_num_args();
$arg_list = func_get_args();
for($n = 0; $n < $numargs; $n++){
$x = preg_match("/^[0-9]*$/", $arg_list[$n]);
if($x == false){
return false;
} else if($n == $numargs - 1){
return true;
}
}
}
function alphanumeric_check(){
$numargs = func_num_args();
$arg_list = func_get_args();
for($n = 0; $n < $numargs; $n++){
$x = preg_match("/^[a-zA-Z0-9\s]*$/", $arg_list[$n]);
if($x == false){
return false;
} else if($n == $numargs - 1){
return true;
}
}
}
function not_empty(){
$n = func_num_args();
$args = func_get_args();
for($i = 0; $i < $n; $i++){
$x = !empty($args[$i]);
if($x == false){
return false;
} else if($i == $n - 1){
return true;
}
}
}
$a = "voda";
$b = 17;
$c = "coding101";
$d = NULL;
echo "Alphabetic check: " . (alphabetic_check($a, $b, $c) ? "true" : "false") . "<br /><br />";
echo "Alphanumeric check: " . (alphanumeric_check($a, $b, $c) ? "true" : "false") . "<br /><br />";
echo "Numeric check: " . (numeric_check($a, $b, $c) ? "true" : "false") . "<br /><br />";
echo "Not empty: " . (not_empty($a, $b, $c) ? "true" : "false") . "<br /><br />";
echo "Not empty 2: " . (not_empty($a, $b, $c, $d) ? "true" : "false") . "<br /><br />";
?>
我可以看到几个问题:
preg_比赛(“^[a-zA-Z\s]*$/”
,开头缺少正斜杠,应该是:preg_比赛(“/^[a-zA-Z\s]*$/”
。这适用于所有preg_比赛,因此会引发错误
$firstname
function alphabetic_check(){
$arg_list = func_get_args();
$valid = true;
foreach($arg_list as $arg){
if(!preg_match("/^[a-zA-Z\s]*$/", $arg))
{
$valid = false;
break;
}
}
return $valid;
}
在我的版本中,我们将一个变量$valid设置为true。如果任何preg_匹配失败,我们将valid设置为false并中断循环。这样,就不需要检查您是否在列表中的最后一个参数上。因此,您也可以改为foreach循环,并且不需要计算传入的参数数量。先生,我很抱歉,但我需要如果您不理解代码,只有当最后一个参数的最后一个preg_匹配为true时,函数才会返回true和false,即使只有一个值不满足preg-match公式,您将如何执行此任务?我了解您的代码的功能,但显然您不了解。“即使只有一个值不满足,也返回false”-是的,因为return就是这样做的-它会立即退出函数。因此,如果在第一次不成功的测试中
返回false
,则循环将不会继续,所有进一步的项目将不再被检查。如果您想要true
的结果,则只有当所有测试都通过时,才可以在第一次失败后返回falsese one-但是如果返回true
,您需要等待检查所有项目。确切地说,所有进一步的项目都不会被检查,这是为了防止潜在的SQL注入,如果您看到第9行到第11行的代码,您将看到…拒绝数据,因为它没有通过某种验证,以及阻止SQL注入,这是两个com非常感谢你的回答。不幸的是,我有很多打字错误,我会找到一种方法来解决这个问题,祝你有愉快的一天,我会留下一些示例来说明工作代码。