help with eregi!

Associate
Joined
19 Oct 2002
Posts
1,925
Location
Welling/London
Am using the following code for email address validation on contact:

!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$", $email

It no longer works as ereg has been deprecated, can anybody help me with an alternative?
 
am trying to use the following but it sees any email address I enter as invalid:

!preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$/i", $email

Any ideas?
 
Last edited:
The ! will reverse the result, i.e. good emails will appear as bad, and bad emails will appear as good.

Try removing it:
PHP:
if (preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$/i", $email)) {
 echo "Ok";
}
else {
 echo "Bad";
}
 
That pattern could be better. You've got fullstops included in a range block, as well as after it?

There's so many email regex patterns on the web, why reinvent the wheel? :)
 
Thanks for your input guys, the reason for the ! is because the code runs "bad email address result, else good email address result"

I know the regex is not great but I am just playing with it at the moment, I have actually found that the code works fine on my own webspace but not on my friends webspace even though it was working last month :confused: Any ideas?

Edit - That link is a good read Pho, thanks :)
 
Last edited:
Do both sites use different versions of php? Run <?php phpinfo(); ?> on both sites.
 
Working site is version - 5.2.12
non working site version - 5.2.11

However I have solved the issue, still not entirely sure why though. The problem was that the form was using $HTTP_POST_VARS for the email address, changed this to $_POST and now it works!? :confused:
 
Last edited:
Working site is version - 5.2.12
non working site version - 5.2.11

However I have solved the issue, still not entirely sure why though. The problem was that the form was using $HTTP_POST_VARS for the email address, changed this to $_POST and now it works!? :confused:

HTTP_*_VARS were deprecated in PHP 4.1 (2001) most hosts using PHP 5 have register_long_arrays disabled in php.ini so they don't work.
 
Last edited:
Ahhhhhhhhhhhhhhhhhhh! Thank you! :)

I knew it was deprecated but couldn't understand why it worked on a newer version of PHP but not the older, all is now clear! You can probably tell I'm new to PHP :D.
 
Back
Top Bottom