我目前使用下面的代碼來實現網址中的頁面,節和類變量。使用php將URL分割爲頁面和文件夾變量?
$domain = 'http://' . $_SERVER['HTTP_HOST'];
$path = $_SERVER['REQUEST_URI'];
$url = $domain . $path;
// page + section + class
$page = basename($url);
$page = $class = str_replace('.php','',$page);
$page = str_replace('-',' ',$page);
if ($path == "/") {
$section = $class = "home";
} else if (basename(dirname($url),"/") == $_SERVER['HTTP_HOST']) {
$section = $page;
} else {
$section = basename(dirname($url),"/");
$section = str_replace('-',' ',$section);
$class = basename(dirname($url),"/") . " " . $class;
}
例如,如果URL是http://www.mydomain.co.uk/about/
代碼將返回以下變量:
$page = "about"
$section = "about"
$class = "about"
對於http://www.mydomain.co.uk/about/general-info/
$page = "general info"
$section = "about"
$class = "about general-info"
但是當我添加更多的深度例如爲http://www.mydomain.co.uk/about/general-info/history/
代碼生產:
$page = "history"
$section = "general info"
$class = "general-info history"
理想的地方我需要它輸出如下:
$page = "history"
$section = "about general info"
$class = "about general-info history"
或擊穿節放到儘可能多的需要,例如:
$section1 = "about"
$section2 = "general-info"
希望有人可以提供幫助。如果有什麼不清楚的地方,請詢問。
請問可以告訴我,直到網址會有多少層次?像這裏你已經提供了直到第三級。 – 2011-05-26 13:37:11
在大多數情況下,它只會持續到第3天。它可能會去第四次,但目前第三 – 2011-05-26 13:39:09
檢查費迪南德的第一個答案。它也是我喜歡的。 – 2011-05-26 13:41:10