【问题标题】:PHP Form Validation - Deprecated Function eregi()PHP 表单验证 - 已弃用的函数 eregi()
【发布时间】:2021-11-20 08:35:27
【问题描述】:

我刚刚针对具有已弃用的 eregi PHP 函数的查询表单的问题提出了一个单独的问题。不幸的是,我错过了另一个文件来验证充斥着它们的表单:(

下面是代码:

   /* ERRORS */
    function error($str) // private
    {
        $this->error = true;
        $this->error_string .= $str;
    } 
    /* VALIDATE FIELD AGAINST TYPE */
    function checkit($value, $type) // private
    {
        $length = "";
        if (eregi("^MIN[0-9]+$", $type)) {
            $tmp = explode(":", $type);
            $length = $tmp[1];
            $type = "MINLENGTH";
        } 
        if (eregi("^MAX[0-9]+$", $type)) {
            $tmp = explode(":", $type);
            $length = $tmp[1];
            $type = "MAXLENGTH";
        } 

        switch ($type) {
            case "NOT_EMPTY":
                $this->error_tmp = "string cannot be empty";
                return $this->not_empty($value);
                break;

            case "MINLENGTH":
                if (strlen($value) < $length) {
                    $this->error_tmp = "string to short";
                    return false;
                } else {
                    return true;
                } 
                break;

            case "MAXLENGTH":
                if (strlen($value) > $length) {
                    $this->error_tmp = "string to long";
                    return false;
                } else {
                    return true;
                } 
                break;

            case "ALPHA":
                $exp = "^[a-z]+$";
                if ($this->not_empty($value) && eregi($exp, $value)) {
                    return true;
                } else {
                    $this->error_tmp = "string not alpha";
                    return false;
                } 
                break;

            case "ALPHASPACE":
                $exp = "^[a-z ]+$";
                if ($this->not_empty($value) && eregi($exp, $value)) {
                    return true;
                } else {
                    $this->error_tmp = "string not alphaspace";
                    return false;
                } 
                break;

            case "ALPHANUM":
                $exp = "^[a-z0-9]+$";
                if ($this->not_empty($value) && eregi($exp, $value)) {
                    return true;
                } else {
                    $this->error_tmp = "string not alphanum";
                    return false;
                } 
                break;

            case "ALPHANUMSPACE":
                $exp = "^[a-z0-9 ]+$";
                if ($this->not_empty($value) && eregi($exp, $value)) {
                    return true;
                } else {
                    $this->error_tmp = "string not alphanumspace";
                    return false;
                } 
                break;

            case "NUMERIC":
                $exp = "^[0-9]+$";
                if ($this->not_empty($value) && eregi($exp, $value)) {
                    return true;
                } else {
                    $this->error_tmp = "string not numeric";
                    return false;
                } 
                break;

            case "NUMERICPLUS":
                $exp = "^[0-9+-.]+$";
                if ($this->not_empty($value) && eregi($exp, $value)) {
                    return true;
                } else {
                    $this->error_tmp = "string not numericplus";
                    return false;
                } 
                break;

            case "EMAIL":
                $exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
                if ($this->not_empty($value) && eregi($exp, $value)) {
                    return true;
                } else {
                    $this->error_tmp = "not a valid email";
                    return false;
                } 
                break;

            case "YYYYMMDD":
                $exp = "^(19|20)[0-9][0-9][- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$";
                if ($this->not_empty($value) && eregi($exp, $value)) {
                    return true;
                } else {
                    $this->error_tmp = "string not YYYYMMDD";
                    return false;
                } 
                break;

            case "DDMMYYYY":
                $exp = "^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)[0-9][0-9]$";
                if ($this->not_empty($value) && eregi($exp, $value)) {
                    return true;
                } else {
                    $this->error_tmp = "string not DDMMYYYY";
                    return false;
                } 
                break;

            case "MMDDYYYY":
                $exp = "^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9][0-9]$";
                if ($this->not_empty($value) && eregi($exp, $value)) {
                    return true;
                } else {
                    $this->error_tmp = "string not MMDDYYYY";
                    return false;
                } 
                break;

            default:
                if ($this->not_empty($value) && $this->regex($type, $value)) {
                    return true;
                } else {
                    $this->error_tmp = "string not valid";
                    return false;
                } 
        } 
    } 
    /* NOT_EMPTY */
    function not_empty($value) // private
    {
        if (trim($value) == "") {
            return false;
        } else {
            return true;
        } 
    } 

    /* REGULAR EXPRESSION */
    function regex($regex, $value) // private
    {
        $the_regex = 'ereg("' . $regex . '", "' . $value . '")';
        $the_code = '<?php if(' . $the_regex . ') { return true; } else { return false; } ?>';
        if (!eval('?>' . $the_code . '<?php ')) {
            return false;
        } else {
            return true;
        } 
    } 
}

是否有太多需要更改的地方?

希望有人能帮忙?

提前致谢,问候

布赖恩

【问题讨论】:

  • Are there too many to change?...不知道你的意思。如果你有时间,你可以改变它们。不清楚你想从我们这里得到什么。
  • 顺便说一句,弃用并不会阻止某些东西的工作......它只会提供一个警告,表明它将在 PHP 的某些未来版本中停止工作 - 这样做是为了让您有足够的时间来计划代替它。当然,如果您现在使用的是最终删除了已弃用代码的版本,那么它将停止工作……但这不再是弃用问题,而是“此功能不存在”问题。您实际遇到的是哪个问题?
  • 对于它的价值,eregi() 是一个 PHP/4 的保留,在 2009 年被弃用 (PHP/5.3) 并在 2015 年完全删除 (PHP/7)。如果我没记错的话,“迁移到 PHP/5”手册一章中有一个指南,但它已被离线。
  • @Brian 如果答案修复了您的代码,请将其作为解决方案!谢谢

标签: php deprecated eregi


【解决方案1】:

Regex 具有“不区分大小写”的方式来匹配字符串。如果将字母"i" 放在正则表达式的末尾,即使您在大写字符串中搜索小写句子,函数 preg_match() 也会匹配该字符串。

ALPHA的情况下,可以使用这个正则表达式:

$exp = "/^[a-z]+$/i";

而不是

$exp = "^[a-z]+$";

使用它,您可以将 PHP 函数从 eregi($exp, $value) 更改为 preg_match($exp, $value),如果有匹配项,它将返回 TRUE

您可以在此处阅读 preg_match() 函数的相关文档:https://www.php.net/manual/en/function.preg-match.php

安德烈亚

【讨论】:

    猜你喜欢
    • 2011-04-14
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 2014-03-17
    • 2013-08-22
    • 1970-01-01
    相关资源
    最近更新 更多