PHP-解析当前URL [英] PHP - parse current URL
本文介绍了PHP-解析当前URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在任何一种情况下,我都需要解析当前网址,以便
I need to parse the current url so that, in either of these cases:
http://mydomain.com/abc/
http://www.mydomain.com/abc/
我可以获得"abc"的返回值(或该位置的任何文本).我该怎么办?
I can get the return value of "abc" (or whatever text is in that position). How can I do that?
推荐答案
您可以使用 parse_url();
$url = 'http://www.mydomain.com/abc/';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
这会给你
Array
(
[scheme] => http
[host] => www.mydomain.com
[path] => /abc/
)
/abc/
更新:获取当前页面的网址,然后对其进行解析:
Update: to get current page url and then parse it:
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
print_r(parse_url(curPageURL()));
echo parse_url($url, PHP_URL_PATH);
这篇关于PHP-解析当前URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文