Alternative to eregi() in php [duplicate]
Asked Answered
D

1

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

Daughterly answered 23/2, 2013 at 22:18 Comment(1)
How to validate an email address in PHPJericajericho
C
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';
Caffrey answered 23/2, 2013 at 22:19 Comment(4)
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)Daughterly
@Nicholas for email validation better use filter_var() see my answer. I'm updated itsCaffrey
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 :)Daughterly
@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'])))Caffrey

© 2022 - 2024 — McMap. All rights reserved.