note 106731 modified in function.parse-url by danbrown
| From: | danbrown@php.net | Date: | Fri, 02 Dec 2011 16:04:33 +0000 |
| Subject: | note 106731 modified in function.parse-url by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-184858@lists.php.net to get a copy of this message | ||
[If you haven't yet] been able to find a simple conversion back to string from a parsed url,
here's an example:
<?php
$url = 'http://usr:pss@example.com:81/mypath/myfile.html?a=b&b[]=2&b[]=3#myfragment';
if ($url === unparse_url(parse_url($url))) {
print "YES, they match!\n";
}
function unparse_url($parsed_url) {
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] .
'://' : '';
$host = isset($parsed_url['host']) ? $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";
}
?>
--was--
Has anyone been able to find a simple conversion back to string from a parsed url? If not,
here's an example:
<?php
$url = 'http://usr:pss@example.com:81/mypath/myfile.html?a=b&b[]=2&b[]=3#myfragment';
if ($url === unparse_url(parse_url($url))) {
print "YES, they match!\n";
}
function unparse_url($parsed_url) {
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] .
'://' : '';
$host = isset($parsed_url['host']) ? $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";
}
?>
http://php.net/manual/en/function.parse-url.php