0
我想在樹枝中創建動態的麪包屑導航。 示例url www.example.com/section/page
我想獲取url,將其分割爲麪包屑,例如:home > section > page
我找到了這個可以工作的php代碼。任何人都可以幫助它轉換成樹枝?如何在樹枝中創建動態的麪包屑導航
<?php
// This function will take $_SERVER['REQUEST_URI'] and build a breadcrumb based on the user's current path
function breadcrumbs($separator = ' » ', $home = 'Home') {
// This gets the REQUEST_URI (/path/to/file.php), splits the string (using '/') into an array, and then filters out any empty values
$path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
// This will build our "base URL" ... Also accounts for HTTPS :)
$base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
// Initialize a temporary array with our breadcrumbs. (starting with our home page, which I'm assuming will be the base URL)
$breadcrumbs = array("<a href=\"$base\">".$home."</a>");
// Find out the index for the last value in our path array
//$last = end(array_keys($path));
// Build the rest of the breadcrumbs
foreach ($path AS $x => $crumb) {
// Our "title" is the text that will be displayed (strip out .php and turn '_' into a space)
$title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));
// If we are not on the last index, then display an <a> tag
if ($x != $last)
$breadcrumbs[] = "<a href=\"".$base.$crumb."\">".$title."</a>";
// Otherwise, just display the title (minus)
else
$breadcrumbs[] = $title;
}
// Build our temporary array (pieces of bread) into one big string :)
return implode($separator, $breadcrumbs);
}
?>
<p><?= breadcrumbs() ?></p>
<p><?= breadcrumbs(' > ') ?></p>
<p><?= breadcrumbs(' ^^ ', 'Index') ?></p>
取得了一些進展...'{%組末頁= '嗒嗒' %}
\t- home
\t - {{ page }}
\t - {{末頁}}
'不能工作,如何獲得,雖然當前頁面的名稱。需要弄清楚如何做一個'foreach'語句... – user3464091這個http://stackoverflow.com/questions/2594211/php-simple-dynamic-breadcrumb?rq=1可能有幫助嗎? – user3464091