PHP Parse URL - Domain returned as path when protocol prefix not present
parse-url, php
Solution
This gives the right results but the file needs to start with a slash:
parse('http://www.domainname.com/');
parse('www.domain.com');
parse('/test/');
parse("/file.php");
function parse($url){
if(strpos($url,"://")===false && substr($url,0,1)!="/") $url = "http://".$url;
$info = parse_url($url);
if($info)
print_r($info);
}
and the result is :
Array
(
[scheme] => http
[host] => www.domainname.com
[path] => /
)
Array
(
[scheme] => http
[host] => www.domain.com
)
Array
(
[path] => /test/
)
Array
(
[path] => /file.php
)
Problem
I am trying to parse URL's in PHP where the input could be any of the following: Code: ``` $info = parse_url('http://www.domainname.com/'); print_r($info); $info = parse_url('www.domain.com'); print_r($info); $info = parse_url('/test/'); print_r($info); $info = parse_url('test.php'); print_r($info); ``` Returns: ``` Array ( [scheme] => http [host] => www.domainname.com [path] => / ) Array ( [path] => www.domain.com ) Array ( [path] => /test/ ) Array ( [path] => test.php ) ``` The problem you can see is the second example where the domain is returned as a path.