views:

49

answers:

3

I have a class that uses PHP's ereg() which is deprecated. Looking on PHP.net I thought I could just get away and change to preg_match()

But I get errors with the regular expressions or they fail!!

Here are two examples:

function input_login() {
    if (ereg("[^-_@\.A-Za-z0-9]", $input_value)) { // WORKS
    // if (preg_match("[^-_@\.A-Za-z0-9]", $input_value)) { // FAILS
        echo "is NOT alphanumeric with characters - _ @ . allowed";
    }
}

// validate email
function validate_email($email) {
    // return eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $email); // FAILS
}
+3  A: 

You forgot the delimiters:

if (preg_match("/[^[email protected]]/", $input_value))

Also, the dot doesn't need to be escaped inside a character class.

For your validation function, you need to make the regex case-insensitive by using the i modifier:

return preg_match('/^[_.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i', $email)
Tim Pietzcker
A: 

try preg_match("/[^-_@\.A-Za-z0-9]/", $input_value)

BojanG
+1  A: 

I can't suppress the suspicion anymore that people simply don't like my [email protected] email address (I changed the right part only). It is an absolutely normal address, it's valid, short and easy to type. Simply people don't like it! One cannot register on any page using that mail.

So, why don't be nice and use PHPs Filter extension to verify the mail or to use a PCRE, which definitely allows all valid emails in use (excluding only the @[] ones):

/^[^@]+@(?:[^.]+\.)+[A-Za-z]{2,6}$/

Thanks for saving my email, it's a dieing species!

nikic