PhpSnip.com

User Stats

URL sanity checker and rebuilder

Gets an URL and converts it to standard format (eg. ftp.somewhere.org - ftp://ftp.somewhere.org/). It can distinguish ftp scheme, when the hostname starts with 'ftp'.

Info

 Download  View Source (print view)
 Rating : 4.7  Views : 431

Source Code ( 38 lines )

function urlcheck($url) {
    $charset = "abcdefghijklmnopqrstuvwxyz"
        ."ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        ."0123456789"
        ."./%_-~:@?#&=";
    if (!isset($url) || !is_string($url)
     || !($len = strlen($url)) || strspn($url, $charset)!=$len)
        return false;
    $URL = parse_url($url);
    if (strlen($URL["port"]))
        $port = ":".$URL["port"];
    else
        $port = "";
    if (!strlen($URL["user"]))
        $user = "";
    elseif (strlen($URL["pass"]))
        $user = $URL["user"].":".$URL["pass"]."@";
    else
        $user = $URL["user"]."@";
    if (strlen($URL["host"]))
        $host = $URL["host"];
    else {
        $host = $URL["path"];
        $URL["path"] = "";
    }
    if (strlen($URL["path"]))
        $path = $URL["path"];
    else
        $path = "/";
    if (strlen($URL["scheme"]))
        $scheme = $URL["scheme"];
    elseif (substr($host, 0, 3) == "ftp")
        $scheme = "ftp";
    else
        $scheme = "http";
    $query = $URL["query"].$URL["fragment"];
    return "$scheme://$user$host$port$path$query";
}

Search

Subscribe

  Rss Feeds

Sponsors

Advertise