舉例子如下:
復制代碼 代碼如下:
$url = "http://www.electrictoolbox.com/php-extract-domain-from-full-url/";
$parts = parse_url($url);
輸出:
復制代碼 代碼如下:
Array
(
[scheme] => http
[host] => www.electrictoolbox.com
[path] => /php-extract-domain-from-full-url/
)
又如:
復制代碼 代碼如下:
<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>
輸出:
復制代碼 代碼如下:
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
可以看到,可以很容易分解出一個URL的各個部,那如果要拿指定的部分出來的話也很容易,如
echo parse_url($url, PHP_URL_PATH);
就是在第二個參數中,設定如下的參數:
PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT.