phppreg-matchvalidationeregi

Eregi and preg_match replacement, big error on capcha and email validator


I know there are a lot of questions related to this subject, but after some days of research I didn't find something that could help this particular case, so here it is:

I have replaced the deprecated eregi with preg_match in 2 files of my website, and now the capcha code gives an error on the registration page, even if the code is absolutely correct

On the registration page I have replaced this

function is_valid_username($username) {
    if(!eregi("^[a-z0-9]*$", trim(str_replace(" ","",$username)))) {
        return 0;
    }

with this

if(!preg_match("^[a-z0-9]*$^", trim(str_replace(" ","",$username)))) {
    return 0;
}

And in my second file I have replaced this:

if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
    $result = 0;
}

with this

if(!preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$^", $email)) {
    $result = 0;
}

How can I resolve this issue?


Solution

  • eregi is case-insensitive, so you would need to add the i modifier to the end of your preg_match expression.

    Also, ^ denotes the start of the input and you have used it as the delimiter.

    So this should be more like the original:

    #^[a-z0-9]*$#i
    

    and

    #^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$#i
    

    By the way, I don't know what your captcha code requires exactly, but there are easier ways to verify an email address using filter_var().