Following this page and the code posted by thomas there:
<?php
$url = 'http://usr:pss@example.com:81/mypath/myfile.html?a=b&b[]=2&b[]=3#myfragment';
print append_www_to_host($url);
function append_www_to_host($url) {
$parsed_url = url_parse($url);
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : 'http://';
$host = isset($parsed_url['host']) ? startsWith($parsed_url['host'], 'www.') ? $parsed_url['host'] : 'www.' . $parsed_url['host'] : '';
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
$user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
$pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
$pass = ($user || $pass) ? "$pass@" : '';
$path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
$query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
$fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
return "$scheme$user$pass$host$port$path$query$fragment";
}
function startsWith($haystack, $needle) {
// search backwards starting from haystack length characters from the end
return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
}
?>
Note the line that starts with $host
, which is where the www.
is being prepended. This should print out http://usr:pss@www.example.com:81/mypath/myfile.html?a=b&b[]=2&b[]=3#myfragment
.
That said, I think this is a bad idea for many reasons - you may be invalidating the URL (for example, what if your user entered m.facebook.com
, and now you're storing www.m.facebook.com
, which doesn't exist?).