繁体   English   中英

PHP正则表达式和URL解析

[英]PHP regular expression and URL parsing

如何使用PHP中的preg_replace来解析URL。

说我有网址

http://google.com?id=123

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

echo $protocol;

会打印

http

要么

echo $domain;

会打印

google

echo $upperDomain;

版画

com

最后,

echo $rest;

会打印

?id=123

有一个功能: parse_url()

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

打印:

Array
(
    [scheme] => http
    [host] => google.com
    [query] => id=123
)

来自文档的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. 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM