0

如何 preg_replace在 PHP 中使用来解析 url。

说我有网址

http://google.com?id=123

我想访问网址的每一部分,这样

echo $protocol;

会打印

http

或者

echo $domain;

会打印

google

echo $upperDomain;

印刷

com

最后,

echo $rest;

会打印

?id=123
4

2 回答 2

4

有一个功能:parse_url()

$url = 'http://google.com?id=123';    
print_r(parse_url($url));

印刷:

Array
(
    [scheme] => http
    [host] => google.com
    [query] => id=123
)
于 2012-12-18T23:47:55.567 回答
0

parse_url() 来自文档...

 mixed parse_url ( string $url [, int $component = -1 ] )

This function parses a URL and returns an associative array containing any of the   
various components of the URL that are present.

This function is not meant to validate the given URL, it only breaks it up into the 
above listed parts. Partial URLs are also accepted, parse_url() tries its best to   
parse them correctly. 
于 2012-12-18T23:47:32.983 回答