[英]PHP Form Validation - Deprecated Function eregi()
我剛剛提出了一個單獨的問題,涉及一個查詢表單的問題,該表單有一個已棄用的 eregi PHP function。不幸的是,我錯過了另一個文件來驗證充滿它們的表單:(
這是下面的代碼:
/* 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"
放在正則表達式的末尾,function preg_match() 將匹配該字符串,即使您在大寫字符串中搜索小寫句子也是如此。
在這種情況下ALPHA ,您可以使用此正則表達式:
$exp = "/^[a-z]+$/i";
代替
$exp = "^[a-z]+$";
使用它,您可以將 PHP 函數從eregi($exp, $value)
更改為preg_match($exp, $value)
,如果匹配則返回TRUE
。
您可以在這里閱讀preg_match() function的相關文檔: https://www.php.net/manual/en/function.preg-match.php
安德烈亞
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.