php

php Filter var & common regex patterns

|

People … quit messing about with ereg_replace / preg_replace / eregi or other things you know nothing of… use the php filter_var. DO IT.
Commonly found bullshit on the interwebs :

function isValidEmail($email){
    return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)@[a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z]{2,3})$",$email);
}

eregi is deprecated and silly, the above WILL miss some of the weirder or longer email addresses like school/company addresses. make your life simpler :

function isValidEmail($email)    {
    return filter_var($email, FILTER_VALIDATE_EMAIL);
}

while not the most perfect thing, its easier to remember AND add to your code.

More info regarding this : PHP manual Filter Var

Some examples :

/*** validate bools ***/
echo filter_var("true", FILTER_VALIDATE_BOOLEAN);

if(filter_var($url, FILTER_VALIDATE_URL) === FALSE)
        {
        /*** no ***/
        echo "Sorry, $url is not valid!";
        }
else
        {
        /*** yes ***/
        echo "The URL, $url is valid!
";
}

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *