【问题标题】:Eregi() Deprecated php help?Eregi() 不推荐使用的 php 帮助?
【发布时间】:2010-08-22 17:31:38
【问题描述】:

我收到错误:

Deprecated: Function eregi() is deprecated in C:\wamp\www\registration\class.register.php on line 75

用我的代码::

if(empty($this->email) || !eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$',$this->email))

我应该使用什么替代方法以及如何实现它????

【问题讨论】:

    标签: php deprecated


    【解决方案1】:

    正如@Sarfraz 所说,ereg_* 函数已被弃用,您应该改用preg_*。但是在这种情况下,您根本不应该使用正则表达式。有一个名为filter_var() 的函数允许您验证一些流行的数据格式(电子邮件、URL 等)

    if (empty($this->email) || false == filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
        // Empty or not valid email
    }
    

    【讨论】:

      【解决方案2】:

      是的ereg 家族函数have been deprecated,您需要改用preg 家族函数。在您的情况下,您应该改用preg_match

      那段代码相当于:

      if(empty($this->email) || 
          !preg_match('~^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$~i',
          $this->email))
      

      也可以压缩成:

      if(empty($this->email) || !preg_match('~^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,4}$~i',
          $this->email))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-26
        • 2011-02-09
        • 1970-01-01
        • 1970-01-01
        • 2021-11-20
        • 2012-12-03
        • 1970-01-01
        相关资源
        最近更新 更多