hi
i want something like this
the user enter a website link
i need check the link if the link doesn't start with 'http://' I want to append 'http://' to the link .
how can I do that in PHP ?
hi
i want something like this
the user enter a website link
i need check the link if the link doesn't start with 'http://' I want to append 'http://' to the link .
how can I do that in PHP ?
if (0 !== stripos($url, 'http://')) {
$url = 'http://' . $url;
}
I'll recommend a slight improvement over tom's
if (0 !== stripos($url, 'http://') && 0 !== stripos($url, 'https://')) { $url = 'http://' . $url; }
However, this will mess up links that use other protocols (ftp:// svn:// gopher:// etc)
if (!preg_match("/^http:\/{2}/",$url)){
$url = 'http://' . $url;
}
I would check for the some letters followed by a colon. According to the URI specifications a colon is used to separate the "schema" (http, ftp etc.) from the "schema specific part". This way if somebody enters (for example) mailto links these are handled correctly.