0

我有两个函数做 preg_match 来检查 emp# 和电子邮件是否有效。如果一个或两个无效,它将被打印到 error.log 但我想以一种方式组织它嗯....让我看看我是否知道如何尽可能简单地举例。

假设如果 emp# 无效,则错误日志将显示

date()
emp#

如果电子邮件无效,则会显示错误日志

date()
email

如果两者都无效,则错误日志将显示

date()
emp#
email

问题是当这两种情况都发生时,我不希望它打印日期两次或多个 emp# 或电子邮件无效日期不会重复只会打印

date()
emp#
emp#
emp$

希望我的解释是有道理的......我现在拥有的是

    if(((isEmailAddressWellFormed($column[3]) == false)) && ((isStudentNumberWellFormed($column[0]) == false)))
    {
        $filehandle = fopen("./courses/path/error.log","a+") or die ("File can not be opened");
        fwrite($filehandle, date("F t, Y (h:i:s a)") . PHP_EOL);
        fwrite($filehandle, "Improper email address from " . $_GET["filename"] . " :" . PHP_EOL);
        fwrite($filehandle, "$column[2] $column[1] $column[3]\n" . PHP_EOL);
        fwrite($filehandle, "Improper student numbers from " . $_GET["filename"] . " :" . PHP_EOL);
        fwrite($filehandle, "$column[2] $column[1] $column[0]\n" . PHP_EOL);
        fclose($filehandle);        
    }
    else
{
    if(isEmailAddressWellFormed($column[3]) == false)
    {
        $filehandle = fopen("./courses/path/error.log","a+") or die ("File can not be opened");
        fwrite($filehandle, date("F t, Y (h:i:s a)") . PHP_EOL);
        fwrite($filehandle, "Improper email address from " . $_GET["filename"] . " :" . PHP_EOL);
        fwrite($filehandle, "$column[2] $column[1] $column[3]\n" . PHP_EOL);
        fclose($filehandle);
    }

    if(isStudentNumberWellFormed($column[0]) == false)
    {
        $filehandle = fopen("./courses/path/error.log","a+") or die ("File can not be opened");
        fwrite($filehandle, date("F t, Y (h:i:s a)") . PHP_EOL);
        fwrite($filehandle, "Improper student numbers from " . $_GET["filename"] . " :" . PHP_EOL);
        fwrite($filehandle, "$column[2] $column[1] $column[0]\n" . PHP_EOL);
        fclose($filehandle);
    }
}

但我知道它没有得到我想要的……有什么简单的方法可以实现吗?

4

3 回答 3

1
$errors = array();

if(!isEmailAddressWellFormed($column[3]))
{
    $errors[] = "Improper email address from " . $_GET["filename"] . " :";
    $errors[] = "$column[2] $column[1] $column[3]";
}

if(!isStudentNumberWellFormed($column[0]))
{
    $errors[] = "Improper student numbers from " . $_GET["filename"] . " :";
    $errors[] = "$column[2] $column[1] $column[0]";
}

if (!empty($errors))
{
    $filehandle = fopen("./courses/path/error.log","a+") or die ("File can not be opened");
    fwrite($filehandle, date("F t, Y (h:i:s a)") . PHP_EOL);
    fwrite($filehandle, implode(PHP_EOL, $errors);
    fclose($filehandle);
}
于 2013-02-24T08:04:41.470 回答
0

有很多方法可以解决这个问题。你可以这样做的一种方法是这样的:

$error = false;

if (!isEmailAddressWellFormed($column[3]))
{
    $emailError = "Improper email address from " . $_GET["filename"] . " :" . PHP_EOL . "$column[2] $column[1] $column[3]\n" . PHP_EOL;
    $error = true;
}

if (!isStudentNumberWellFormed($column[0]))
{
    $studentError = "Improper student numbers from " . $_GET["filename"] . " :" . PHP_EOL . "$column[2] $column[1] $column[0]\n" . PHP_EOL;
    $error = true;
}

if ($error)
{
    $handle = fopen("./courses/path/error.log","a+") or die ("File can not be opened");

    fwrite($filehandle, date("F t, Y (h:i:s a)") . PHP_EOL);

    if (isset($emailError))
        fwrite($handle, $emailError);

    if (isset($studentError))
        fwrite($handle, $studentError);

    fclose($handle);
}
于 2013-02-24T08:03:46.070 回答
0

尝试使用日志类,写入错误

https://github.com/soroushatarod/Loggerr

于 2013-02-24T10:11:32.997 回答