Reputation: 27058
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
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
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
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