7

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 :)

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
idjuradj
  • 1,355
  • 6
  • 19
  • 31
  • 1
    [How to validate an email address in PHP](http://stackoverflow.com/questions/12026842/how-to-validate-an-email-address-in-php/12026863#12026863) – PeeHaa Feb 23 '13 at 22:50

1 Answers1

17
 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';
Winston
  • 1,758
  • 2
  • 17
  • 29
  • 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 http://pastebin.com/q7Mfym9q) – idjuradj Feb 23 '13 at 22:30
  • @Nicholas for email validation better use filter_var() see my answer. I'm updated its – Winston Feb 23 '13 at 22:36
  • 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 :) – idjuradj Feb 23 '13 at 22:42
  • @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'])))` – Winston Feb 23 '13 at 22:45