Is there any quick function that will convert: HtTp://www.ExAmPle.com/blah
to http://www.example.com/blah
Basically I want to lower case the case-insen开发者_StackOverflow中文版sitive parts of a url.
No, you'll have to write code for it on your own.
But you can use parse_url()
to split the URL into its parts.
Since you asked for "quick," here's a one-liner that does the job:
$url = 'HtTp://User:Pass@www.ExAmPle.com:80/Blah';
echo preg_replace_callback(
'#(^[a-z]+://)(.+@)?([^/]+)(.*)$#i',
create_function('$m',
'return strtolower($m[1]).$m[2].strtolower($m[3]).$m[4];'),
$url);
Outputs:
http://User:Pass@www.example.com:80/Blah
EDIT/ADD:
I've tested, and this version is about 55% faster than using preg_replace_callback with an anonymous function:
echo preg_replace(
'#(^[a-z]+://)(.+@)?([^/]+)(.*)$#ei',
"strtolower('\\1').'\\2'.strtolower('\\3').'\\4'",
$url);
I believe this class
will do what you're looking for http://www.glenscott.co.uk/blog/2011/01/09/normalize-urls-with-php/
Here's a solution, expanding on what @ThiefMaster already mentioned:
DEMO
function urltolower($url){
if (($_url = parse_url($url)) !== false){ // valid url
$newUrl = strtolower($_url['scheme']) . "://";
if ($_url['user'] && $_url['pass'])
$newUrl .= $_url['user'] . ":" . $_url['pass'] . "@";
$newUrl .= strtolower($_url['host']) . $_url['path'];
if ($_url['query'])
$newUrl .= "?" . $_url['query'];
if ($_url['fragment'])
$newUrl .= "#" . $_url['fragment'];
return $newUrl;
}
return $url; // could return false if you'd like
}
Note: Not battle-tested but it should get you going.
精彩评论