2

我从某人那里得到了这段代码,创建动态面包屑几乎是完美的,但是有一点小故障,因为它在面包屑之前呼应了两个分隔线:

$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
foreach($crumbs as $crumb){
    echo ucfirst(str_replace(array(".php","_"),array(""," "),'>' . $crumb));
}

它呼应:

“>>内容>常用>文件”

我希望它看起来像

“内容>常见> 1”

如果有人能告诉我如何为数组中除最后一个(文件)之外的所有项目添加链接,我将不胜感激?

非常感谢大家,这个网站真的帮助了我很多通过例子学习php!

4

1 回答 1

2

也许这样的事情会做:

//去掉空白部分 $crumbs = array_filter($crumbs);

$result = array();
$path = '';
foreach($crumbs as $crumb){
    $path .= '/' . $crumb;
    $name = ucfirst(str_replace(array(".php","_"),array(""," "), $crumb));
    $result[] = "<a href=\"$path\">$name</a>";

}

echo implode(' > ', $result);

更新

$result = array();
$path = '';
$num = count($crumbs);
for ($j=0; $j<$num; $j++) {
    $crumb = $crumbs[$j];
    if ($crumb == '') {
        continue;   
    }
    $path .= '/' . $crumb;
    $name = ucfirst(str_replace(array(".php","_"),array(""," "), $crumb));
    if ($j < ($num - 1)) {
        $result[] = "<a href=\"$path\">$name</a>";
    } else {
        $result[] = $name;
    }
}

echo implode(' > ', $result);
于 2010-04-08T10:56:10.397 回答