2010-03-12 96 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