note 120151 deleted from function.parse-url by cmb
| From: | cmb@php.net | Date: | Sun, 06 Oct 2019 08:16:29 +0000 |
| Subject: | note 120151 deleted from function.parse-url by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212503@lists.php.net to get a copy of this message | ||
Note Submitter: mpyw
----
Yet another URL builder implementation:
<?php
function build_url(array $elements)
{
$e = $elements;
return
(isset($e['host']) ? (
(isset($e['scheme']) ? "$e[scheme]://" : '//') .
(isset($e['user']) ? $e['user'] . (isset($e['pass']) ?
":$e[pass]" : '') . '@' : '') .
$e['host'] .
(isset($e['port']) ? ":$e[port]" : '')
) : '') .
(isset($e['path']) ? $e['path'] : '/') .
(isset($e['query']) ? '?' . (is_array($e['query']) ?
http_build_query($e['query'], '', '&') : $e['query']) :
'') .
(isset($e['fragment']) ? "#$e[fragment]" : '')
;
}
?>
For convenience, you can pass $elements['query'] as array.
Example:
<?php
$elements = [
"host" => "example.com",
"user" => "root",
"pass" => "stupid",
"path" => "/x/y/z",
"query" => [
"a" => "b",
"c" => "d",
],
];
echo build_url($elements);
?>
will output
//root:stupid@example.com/x/y/z?a=b&c=d