另一個FPGA實現:
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]" : '')
;
}
結果應該是:
{
"host": "example.com"
}
/* //example.com/ */
{
"scheme": "https",
"host": "example.com"
}
/* https://example.com/ */
{
"scheme": "http",
"host": "example.com",
"port": 8080,
"path": "/x/y/z"
}
/* http://example.com:8080/x/y/z */
{
"scheme": "http",
"host": "example.com",
"port": 8080,
"user": "anonymous",
"query": "a=b&c=d",
"fragment": "xyz"
}
/* http://[email protected]:8080/?a=b&c=d#xyz */
{
"scheme": "http",
"host": "example.com",
"user": "root",
"pass": "stupid",
"path": "/x/y/z",
"query": {
"a": "b",
"c": "d"
}
}
/* http://root:[email protected]/x/y/z?a=b&c=d */
{
"path": "/x/y/z",
"query": "a=b&c=d"
}
/* /x/y/z?a=b&c=d */
我看看`http_build_url`,但是看起來像這樣一個麻煩,如果網址不同有其他屬性沒有提及'$ url` – Val 2010-12-04 17:52:08
既不是`www.example.com`也不是`example.com`絕對有效網址;將被解釋爲URL路徑。 – Gumbo 2010-12-04 18:07:59