如果正斜杠后没有任何内容,PHP获取当前目录

问题描述:

I have a site that uses ad banner rotators and I'm assigning them to different sections of a site depending on the current directory of the URL. BUT, if you're in the "root" of the site, meaning, the home page "www.somedomain.com" and there's no other directory, I want to assign it as such.

Right now, the only thing I can think of is this:

$SiteDirectory = explode('/',$url);

Which returns current directory after the last "/" of a URL.

But what if there's nothing after it? How do I identify if it is the root of the site?

This:

$Site = parse_url($url);

just gets me the domain, but if there's directories after it, it's useless to me.

And this:

$CurrentURL = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

gives me the the WHOLE thing....

So, how do I find out if it's the home directory?

我有一个使用广告横幅旋转器的网站,我将它们分配到网站的不同部分,具体取决于 URL的当前目录。 但是,如果你在网站的“根”,意思是主页“www.somedomain.com”,而且没有其他目录,我想这样分配它。 p> 现在,我唯一能想到的是: p>

  $ SiteDirectory = explode('/',$ url); 
  code>   pre> 
 
 

在URL的最后一个“/”之后返回当前目录。 p>

但如果之后什么也没有呢? 如何识别它是否是站点的根目录? p>

这个: p>

  $ Site = parse_url($ url); \  n  code>  pre> 
 
 

只是让我获得了域名,但是如果它后面有目录,那对我来说就没用了。 p>

这就是: p>

  $ CurrentURL =“http:// $ _ SERVER [HTTP_HOST] $ _SERVER [REQUEST_URI]”; 
  code>  pre> 
 
 

给出 我是整个事情...... p>

那么,我怎么知道它是否是主目录? p> div>

if there is something after '/' than $SiteDirectory array count must be greater than one as $SiteDirectory[0] contain domain name and indexes after that having sub directories

$SiteDirectory = array_filter(explode('/',$url));
if(0 == count($SiteDirectory)){
   // something is wrong
}else{
    if(1 == count($SiteDirectory))
    {
        // you are on the root
    }else{
        $last_directory = $SiteDirectory[count($SiteDirectory) - 1];
    }
}