How can I utilize preg_replace
in PHP to parse a url.
Say I have the url
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()
$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...
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.