簡體   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