Example how to add http:// to the url if there isn’t a http:// or https:// or ftp://

This is a simple php example how to add http:// to the url.

function addhttp($url) {
    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
        $url = "http://" . $url;
    return $url;

Recognizes ftp://, ftps://, http:// and https:// in a case insensitive way.

Leave a Reply

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

twenty − 13 =