PHP 表单验证 - 弃用函数 eregi()
PHP Form Validation - Deprecated Function eregi()
我刚刚提出了一个单独的问题,涉及一个查询表单的问题,该表单具有已弃用的 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;
}
}
}
是不是太多要改了?
希望有人能帮忙?
在此先感谢,亲切的问候
布莱恩
正则表达式以“不区分大小写”的方式匹配字符串。如果将字母 "i"
放在正则表达式的末尾,函数 preg_match() 将匹配该字符串,即使您在大写字符串中搜索小写句子。
在ALPHA的情况下,可以使用这个正则表达式:
$exp = "/^[a-z]+$/i";
而不是
$exp = "^[a-z]+$";
使用它,您可以将 PHP 函数从 eregi($exp, $value)
更改为 preg_match($exp, $value)
,如果有匹配,这将 return TRUE
。
您可以在这里阅读preg_match()函数的相关文档:https://www.php.net/manual/en/function.preg-match.php
安德里亚
我刚刚提出了一个单独的问题,涉及一个查询表单的问题,该表单具有已弃用的 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;
}
}
}
是不是太多要改了?
希望有人能帮忙?
在此先感谢,亲切的问候
布莱恩
正则表达式以“不区分大小写”的方式匹配字符串。如果将字母 "i"
放在正则表达式的末尾,函数 preg_match() 将匹配该字符串,即使您在大写字符串中搜索小写句子。
在ALPHA的情况下,可以使用这个正则表达式:
$exp = "/^[a-z]+$/i";
而不是
$exp = "^[a-z]+$";
使用它,您可以将 PHP 函数从 eregi($exp, $value)
更改为 preg_match($exp, $value)
,如果有匹配,这将 return TRUE
。
您可以在这里阅读preg_match()函数的相关文档:https://www.php.net/manual/en/function.preg-match.php
安德里亚