$url = "http://www.111cn.net.net/welcome/";
$parts = parse_url($url);
print_r($parts);
/*
array
(
[scheme] => http
[host] => www.111cn.net.net
[path] => /welcome/
)
這樣我們很空間取url各部份內容了。
可以看到,可以很容易分解出一個url的各個部,那如果要拿指定的部分出來的話也很容易,如:
view sourceprint?1 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
*/
看一個復雜一點的函數
function parseurl($url) {
$r = "^(?:(?p<scheme>w+)://)?";
$r .= "(?:(?p<login>w+):(?p<pass>w+)@)?";
$r .= "(?p<host>(?:(?p<subdomain>[-w.]+).)?" . "(?p<domain>[-w]+.(?p<extension>w+)))";
$r .= "(?::(?p<port>d+))?";
$r .= "(?p<path>[w/]*/(?p<file>w+(?:.w+)?)?)?";
$r .= "(?:?(?p<arg>[w=&]+))?";
$r .= "(?:#(?p<anchor>w+))?";
$r = "!$r!"; // delimiters
preg_match ( $r, $url, $out );
return $out;
}