PHP - 解析当前 URL [英] PHP - parse current URL

查看:33
本文介绍了PHP - 解析当前 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析当前的 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/

更新:获取当前页面的url然后解析它:

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);

curPageURL 函数的源代码

这篇关于PHP - 解析当前 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆