Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
西班牙语DNI的PHP检查函数。服务器错误500,为什么?_Php_Regex_If Statement - Fatal编程技术网

西班牙语DNI的PHP检查函数。服务器错误500,为什么?

西班牙语DNI的PHP检查函数。服务器错误500,为什么?,php,regex,if-statement,Php,Regex,If Statement,我绝望了。几天以来,我一直在尝试在PHP中为西班牙DNI实现一个检查函数。我没有发现我的错误。有人能看一下吗 <?php require("bbdd.php"); $errors = ''; $dni = checkDNI($_POST['dni']); /** * Function which checks if a string is a valid spanish DNI * * @param string $dniUnchecked * @return string

我绝望了。几天以来,我一直在尝试在PHP中为西班牙DNI实现一个检查函数。我没有发现我的错误。有人能看一下吗

<?php

require("bbdd.php");

$errors = '';
$dni = checkDNI($_POST['dni']);

/**
 * Function which checks if a string is a valid spanish DNI
 *
 * @param string $dniUnchecked
 * @return string $dni
 */      
function checkDNI($dniUnchecked) {
    //Possible values for the final letter
    $letterValues = array(
        'T' => 0, 'R' => 1, 'W' => 2, 'A' => 3, 'G' => 4, 'M' => 5,
        'Y' => 6, 'F' => 7, 'P' => 8, 'D' => 9, 'X' => 10, 'B' => 11,
        'N' => 12, 'J' => 13, 'Z' => 14, 'S' => 15, 'Q' => 16, 'V' => 17,
        'H' => 18, 'L' => 19, 'C' => 20, 'K' => 21, 'E' => 22
    );
    //Check if entered
    if($dniUnchecked == ''){
        $errors .= 'Please enter a DNI.<br/>';  
        echo $errors;
        return false;
    }       
    //Check length
    elseif(strlen($dniUnchecked) != 9){
        $errors .= 'Please enter a DNI that has 8 digits and a check-letter.<br/>';
        echo $errors;
        return false;
    }
    //Check validity
    elseif (preg_match('/^[0-9]{8}[A-Z]$/i', $dniUnchecked)) {
        //Check letter
        $checkArray = str_split($dniUnchecked);
        $checkArray[8] = $letterValues[$checkArray[8]];
        //var_dump($checkArray);
        $checkSum = 0;
        $i = 0;
        $factor = array(7, 3, 1, 7, 3, 1, 7, 3, 1);
        for($i=0; $i<9; $i++){
            $checkArray[$i] = (int)$checkArray[$i];
            $checkSum += ($checkArray[$i] *= $factor[$i]);
        }
        if((preg_match("/\d$/", $checkSum) == 3) {
        //All was ok
        echo 'all ok';
            $dni = trim($dniUnchecked);
            $dni = stripsplashes($dni);
            $dni = htmlspecialchars($dni);
            return $dni;
        } else {
        $errors .= 'Please enter a valid DNI.<br/>';
        echo $errors;
        }
    }
}
您有一个额外的

但说真的,打开

您可以通过两种方式来实现这一点。例如:

ini_set('display_errors',1); 
error_reporting(E_ALL);

在脚本顶部。

是否打开了PHP错误报告?如果没有,为什么没有?如果是,是否检查了错误日志?是否检查了web服务器错误日志?是否进行了任何调试工作?脚本行为与您预期的有何不同?是的,谢谢!错误报告确实打开了。我删除了额外的错误(.但我的VirtualBox似乎有问题。我不知道问题是什么。有时有效,有时无效。我目前无法测试它……但随着时间的推移,它有望工作。
if((preg_match("/\d$/", $checkSum) == 3) {
// ^
// there
ini_set('display_errors',1); 
error_reporting(E_ALL);