电子邮件验证功能启动错误
Email Validation Function kicking up error
我一直在从 PHP 4 升级到 PHP 5.7,我有一个我一直在开发的功能:
function is_valid_email($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!preg_match("/[^A-Za-z'-]/",$local_array($i))) {
return false;
}
}
if (!preg_match("^\[?[0-9\.]+\]?$",'/' . $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!preg_match("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za- z0-9]+))$", '/' . $domain_array[$i])) {
return false;
}
}
}
return true;
}
提交表单时出现此错误:
Fatal error: Function name must be a string in /usr/local/www/panhistoria/MyBooks/email_alert.php on line 241
第241行是第一个!preg_match
$local_array($i)
是数组,不是函数,所以需要用[]
或{}
.
来寻址
所以尝试:
if (!preg_match("/[^A-Za-z'-]/",$local_array[$i])) {
有关访问数组的详细信息,请参阅:http://php.net/manual/en/language.types.array.php。
来自手册:
Both square brackets and curly braces can be used interchangeably for accessing array elements.
你后面的正则表达式也丢失了 delimiters。
例如你的第二个 preg_match
应该是:
preg_match("/^\[?[0-9\.]+\]?$/"
如果您需要使用修饰符,它将在第二个 /
之后使用。如果 /
需要在表达式中使用,您可以将其转义或更改分隔符。转义将是 \/
。作为不同的分隔符:
preg_match("~^\[?[0-9\.]+\]?$~"
您还应该努力缩进每个控制块。
我一直在从 PHP 4 升级到 PHP 5.7,我有一个我一直在开发的功能:
function is_valid_email($email) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!preg_match("/[^A-Za-z'-]/",$local_array($i))) {
return false;
}
}
if (!preg_match("^\[?[0-9\.]+\]?$",'/' . $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!preg_match("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za- z0-9]+))$", '/' . $domain_array[$i])) {
return false;
}
}
}
return true;
}
提交表单时出现此错误:
Fatal error: Function name must be a string in /usr/local/www/panhistoria/MyBooks/email_alert.php on line 241
第241行是第一个!preg_match
$local_array($i)
是数组,不是函数,所以需要用[]
或{}
.
所以尝试:
if (!preg_match("/[^A-Za-z'-]/",$local_array[$i])) {
有关访问数组的详细信息,请参阅:http://php.net/manual/en/language.types.array.php。
来自手册:
Both square brackets and curly braces can be used interchangeably for accessing array elements.
你后面的正则表达式也丢失了 delimiters。
例如你的第二个 preg_match
应该是:
preg_match("/^\[?[0-9\.]+\]?$/"
如果您需要使用修饰符,它将在第二个 /
之后使用。如果 /
需要在表达式中使用,您可以将其转义或更改分隔符。转义将是 \/
。作为不同的分隔符:
preg_match("~^\[?[0-9\.]+\]?$~"
您还应该努力缩进每个控制块。