【问题标题】:Email Validation Function kicking up error电子邮件验证功能启动错误
【发布时间】:2016-03-06 19:51:48
【问题描述】:

我一直在从 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;
}

提交表单时出现此错误:

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

第241行是第一个!preg_match

【问题讨论】:

  • 只使用内部函数if(filter_var($email, FILTER_VALIDATE_EMAIL)){ ...

标签: php email-validation


【解决方案1】:

$local_array($i) 是一个数组,不是一个函数,所以需要用[]{} 来寻址。

那就试试吧:

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

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

来自手册:

方括号和花括号可以互换使用来访问数组元素。

您以后的正则表达式也缺少delimiters

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

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

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

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

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

【讨论】:

  • 好的 - 太好了,我完全错过了!但我无法理解分隔符 - 它们是斜线吗?放置它们的正确位置在哪里?
  • 添加了几个分隔符使用示例。
猜你喜欢
  • 1970-01-01
  • 2011-01-09
  • 2011-04-22
  • 2014-12-19
  • 1970-01-01
  • 2014-02-15
  • 2019-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多