Patrioticcow
Patrioticcow

Reputation: 27058

php , replace code with php 5.3 compatible code

i need to rewrite this function to be compatible with php 5.3

function fns_data($address)
{
if (ereg("^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $address))
{return true;}
return false;
}

anyone can help?

thanks

Upvotes: 0

Views: 129

Answers (3)

Cyril N.
Cyril N.

Reputation: 39909

First of all, don't use ereg, but preg_replace, and use a correct email regex, like this one :

function fns_data($address) {
    if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $address))
        return true;

    return false;
}

even better, you can now use filters :

function fns_data($address) {
    return filter_var($address, FILTER_VALIDATE_EMAIL);
}

Upvotes: 0

Fosco
Fosco

Reputation: 38526

Check out filter_var(), new to PHP at version 5.2.0:

function fns_data($address)  {  
    return filter_var($address, FILTER_VALIDATE_EMAIL);
}

Upvotes: 3

Marc B
Marc B

Reputation: 360872

In general, changing ereg to preg is (usually) as simple as renaming it to "preg_match" and adding a / to the start and end of the pattern. Since your pattern is (relatively) simple:

function fns_data($address) {
   if (preg_match('/^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/', $address)) {
       return true;
   }
   return false;
}

Upvotes: 1

Related Questions