简体   繁体   中英

Eregi() Deprecated php help?

I am recieving the error:

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

with my code::

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

What alternative should I use and how can I implement it????

As @Sarfraz said ereg_* functions are deprecated and you should use preg_* instead. However in this case you shouldn't use regular expressions at all. There is function called filter_var() that allows you to validate some popular data formats (emails, URLs etc.)

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

Yes ereg family functions have been deprecated , you need to use preg family functions instead. In your case, you should use preg_match instead.

That piece of code is equivalent to:

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

It can also be compacted to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM