Alternative to eregi() in php

29,907
 if (!preg_match("/^[A-Z0-9.%-]+@[A-Z0-9.%-]+.[A-Z]{2,4}$/", trim($_POST['email'])))

Using preg_match.

Because ereg_* functions is deprecated in PHP >= 5.3

Also for email validation better used filter_var

if (!filter_var(trim($_POST['email']), FILTER_VALIDATE_EMAIL))
    echo 'Email is incorrect';
Share:
29,907
idjuradj
Author by

idjuradj

Updated on June 19, 2020

Comments

  • idjuradj
    idjuradj almost 4 years

    So, i was using eregi in my mail script, but as of lately, i get the error that the function is deprecated.

    So, what is the easiest way to replace the following bit of code:

    if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email'])))?
    

    Any help is appreciated :)

  • idjuradj
    idjuradj about 11 years
    Thanks for the reply, i tried it now, but it's not sending mails now. I'm not receiving test mails, and with eregi it works. o.O? Should i change anything else in my mail script? (if you want, you take a look at the script here pastebin.com/q7Mfym9q)
  • Winston
    Winston about 11 years
    @Nicholas for email validation better use filter_var() see my answer. I'm updated its
  • idjuradj
    idjuradj about 11 years
    Hm, thanks for the follow up. I will consider it. In the meantime, i found a solution. When i replace the given line with this if (!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.‌​)+[a-zA-Z]{2,6}$/i", trim($_POST['email']))) { it works :)
  • Winston
    Winston about 11 years
    @Nicholas Even you can reduce your expression like this if(!preg_match("/^[_.\da-z-]+@[a-z\d][a-z\d-]+\.+[a-z]{2,6}$‌​/i", trim($_POST['email'])))