miguel.nz

Check the URL field

June 8, 2014   |   1 minute read.

It’s really common to have a field called URL where the is able to add an url for a blog, facebook, twitter or anything that he wants. The main problem is we/they always forgot to add the HTTP://, so this small snippet helps you solving that.

function checkurl($url){
    if ( (strpos($url,'http://') === false) && (strpos($url,'https://') === false) ){ 
        return 'http://'.$url; 
    }
    
    else return $url;
}

Or, if you don’t want to strictly add http you can leave it with double slash // and it will look something like this:

function checkurl($url){
    if ( (strpos($url,'http://') === false) && (strpos($url,'https://') === false) ){ 
        return '//'.$url; 
    }

    else return $url;
}

You can find loads of different answers here.