如何仅从主页上的面包屑中删除尾部双箭头“»”

时间:2014-11-21 17:20:24

标签: php breadcrumbs

我发现这个有用的PHP片段并进行了一些小的修改,但是我还没有弄清楚如何排除主页在“Home”之后显示尾随的“»”。结果,它看起来像这样... 主页»

有没有一种简单的方法可以在不改变面包屑出现在任何其他页面上的方式来删除它?

<?php

function breadcrumbs($sep = ' &raquo; ', $home = 'Home') {
//Use RDFa breadcrumb, can also be used for microformats etc.
$bc     =   '<div xmlns:v="http://rdf.data-vocabulary.org/#" id="crums">'.$text;
//Get the website:
$site   =   'http://'.$_SERVER['HTTP_HOST'];
//Get all vars en skip the empty ones
$crumbs =   array_filter( explode("/",$_SERVER["REQUEST_URI"]) );
//Create the home breadcrumb
$bc    .=   '<span typeof="v:Breadcrumb"><a href="'.$site.'" rel="v:url" property="v:title">'.$home.'</a>'.$sep.'</span>';
//Count all not empty breadcrumbs
$nm     =   count($crumbs);
$i      =   1;
//Loop the crumbs
foreach($crumbs as $crumb){
    //Make the link look nice
    $link    =  ucfirst( str_replace( array(".php","-","_"), array(""," "," ") ,$crumb) );
    //Loose the last seperator
    $sep     =  $i==$nm?'':$sep;
    //Add crumbs to the root
    $site   .=  '/'.$crumb;
    //Make the next crumb
    $bc     .=  '<span typeof="v:Breadcrumb"><a href="'.$site.'/" rel="v:url" property="v:title">'.$link.'</a>'.$sep.'</span>';
    $i++;
}
$bc .=  '</div>';
//Return the result
return $bc;}
?>

<p><?= breadcrumbs() ?></p>

1 个答案:

答案 0 :(得分:2)

您必须删除每次在“Home”之后放置的分隔符。只有在“Home”之后有东西时才添加它。

$crumbs =   array_filter( explode("/",$_SERVER["REQUEST_URI"]) );

//Count all not empty breadcrumbs
$nm     =   count($crumbs);
$i      =   1;

// Add first separator if there is at least one crumb
$homesep     =  $nm == 0?'':$sep;
//Create the home breadcrumb
$bc    .=   '<span typeof="v:Breadcrumb"><a href="'.$site.'" rel="v:url" property="v:title">'.$home.'</a>'.$homesep.'</span>';

//Loop the crumbs
foreach($crumbs as $crumb){
    ...
}