电子邮件验证功能启动错误


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;
}

提交表单时,我收到此错误:

致命错误:函数名称必须是第 241 行/usr/local/www/panhistoria/MyBooks/email_alert.php 中的字符串

241路是第一!preg_match

$local_array($i)是一个

数组,而不是一个函数,所以它需要用[]{}来寻址。

所以试试:

if (!preg_match("/[^A-Za-z'-]/",$local_array[$i])) {

有关访问数组的详细信息,请参阅:http://php.net/manual/en/language.types.array.php。

从手册:

方括号和大括号可以互换用于访问数组元素。

此外,您后来的正则表达式也缺少分隔符。

例如,您的第二个preg_match应该是:

preg_match("/^'[?[0-9'.]+']?$/"

如果您需要使用修饰符,它将在第二个/之后。如果需要在表达式中使用/,您可以对其进行转义或更改分隔符。逃跑将是'/.作为不同的分隔符:

preg_match("~^'[?[0-9'.]+']?$~"

您还应该努力缩进每个控制块。