2010-03-12 88 views

回答

21

dirname()。您可以使用它很多次,只要你愿意

  • 得到父/子 - 目录名称(“父/子/回复”)
  • 获得母公司 - 目录名(目录名(“父/子/回复'))
2
preg_replace("/\/\w+$/i","",__DIR__); 
    # Note you may also need to add .DIRECTORY_SEPARATOR at the end. 
1

这里是删除URL的最后ñ部位的功能:

/** 
* remove the last `$level` of directories from a path 
* example 'aaa/bbb/ccc' remove 2 levels will return aaa/ 
* 
* @param $path 
* @param $level 
* 
* @return mixed 
*/ 
public function removeLastDir($path, $level) 
{ 
    if (is_int($level) && $level > 0) { 
     $path = preg_replace('#\/[^/]*$#', '', $path); 

     return $this->removeLastDir($path, (int)$level - 1); 
    } 

    return $path; 
} 
+0

真的吗?正则表达式和递归?为什么不用'dirname'作为'for'循环?为了简单? – lxg 2015-06-02 21:48:51