[英]PHP regular expression and URL parsing
How can I utilize preg_replace
in PHP to parse a url. 如何使用PHP中的
preg_replace
来解析URL。
Say I have the url 说我有网址
http://google.com?id=123 http://google.com?id=123
and I want to access each part of the url such that 我想访问网址的每个部分,这样
echo $protocol;
would print 会打印
http
or 要么
echo $domain;
would print 会打印
google
and 和
echo $upperDomain;
prints 版画
com
and finally, 最后,
echo $rest;
would print 会打印
?id=123
there is a function for that: parse_url() 有一个功能: parse_url()
$url = 'http://google.com?id=123';
print_r(parse_url($url));
prints: 打印:
Array
(
[scheme] => http
[host] => google.com
[query] => id=123
)
parse_url() from the docs... 来自文档的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.