Jochem Maas wrote:
Németh Zoltán wrote:
You should first check the url and only add "http://"; at the beginning
if its not there

I do it like

if (substr($url, 0, 4) != "http") {$url = "http://"; . $url;}

in my website, although it is probably not the most elegant solution ;)

it will break in the highly unlikely situation that someone uploads a url like:

        http.mydomain.com

an also for things like:

        ftp://leet.haxordownload.org/

2 alternatives spring to mind:

        1. a beasty little regex.
        2. use the output of parse_url().

the second is by far the better way of doing this

Using parse_url for this seems like overkill to me. Just check the string for the presence of :// and prepend http:// if it's missing.

if (false === strpos($url, '://'))
{
    $url = 'http://'.$url;
}

There will be fringe cases where someone might enter example.com/jumpto?url=http://othersite.org/, in which case do the check up to the first '.'.

if (false === strpos(substr($url, 0, strpos($url, '.'), '://'))
{
    $url = 'http://'.$url;
}

I don't have numbers, but I'd bet this is quicker than doing a parse_url.

-Stut

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to