Need to tweak a preg_replace

I’ve been using this code to help catch when people mis-type their email address. It works great for most email addresses, except for ones that have something after the .com, like john.doe@bigpond.com.au

What is the best way to modify this preg_replace so that it successfully handles domains like big pond.com.au ?

Thanks!


$emailDomain = preg_replace('/^.+?@/', '', $emailSenderAddress).'.';


if (!checkdnsrr($emailDomain, 'MX') && !checkdnsrr($emailDomain, 'A')) {


    $error = "Yes";
    $errorMessage .= "<li>The email address is not setup to accept emails.</li>";


}



Hello busboy!

Here’s the thing with email validation. It’s really more complicated than what it looks like because, well, a lot of characters are supported.
If you read this page from wikipedia: http://en.wikipedia.org/wiki/Email_address
You’ll see that a those characters are valid: ! # $ % & ’ * + - / = ? ^ _ ` { | } ~
And a LOT of other weird stuff is accepted.

Here are some interesting read on email validation in PHP:
http://mattiasgeniar.be/2009/02/07/input-validation-using-filter_var-over-regular-expressions/
http://stackoverflow.com/questions/3370194/filter-var-or-custom-function-for-email-validation-in-php
http://stackoverflow.com/questions/3722831/does-phps-filter-var-filter-validate-email-actually-work

You can use something as easy as this to validate an email:

function is_email($email) {
    return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}

with some drawbacks. See the links I included.

Now, anyway, back to your MAIN question :wink:

I tried this code on phpfiddle.org and it worked:

<?php
//$emailSenderAddress = 'test@test.com.au';	
$emailSenderAddress =  'john.doe@bigpond.com.au';
//$emailSenderAddress =  'john.doe@bigpond.fdsfsdfsd';
$emailDomain = preg_replace('/^.+?@/', '', $emailSenderAddress).'.';

$errorMessage = "OK";

if (!checkdnsrr($emailDomain, 'MX') && !checkdnsrr($emailDomain, 'A')) {   
    echo "BAD";
} else {
	echo "OK";
}
	
?>

only the last URL tested (john.doe@bigpond.fdsfsdfsd) gave me an error…
So maybe you have a problem elsewhere in your code?