Wednesday, March 09, 2005

PHP: Add access protocol to domain name

Trying to find a script that does this. Not to difficult, I suppose, to roll my own, but I'd guess that it's common enough that I can cherry-pick one. Some references:

URL (answers.com)
preg_replace (php.net)
eregi_replace (php.net)
koders code (Google cache)

The result:

/* Fx add_http (1.1)
******************************/
function add_http($url) {

/* Get rid of any extra white space */
$url = trim($url);

/* Get rid of any preceding whitespace (fix for odd browsers like konqueror) */
$url = eregi_replace("^[[:space:]]+", "", $url);

/* If the URI doesn't start with a scheme, prepend 'http://' */
if (!empty($url)) {
if (!ereg("^(http)", $url)) {
$url = "http://$url";
}
}

return $url;

} /* end Fx */
/******************************/

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home