Adr*_* M. 19 php dynamic breadcrumbs
我觉得这个脚本对这里的任何菜鸟都很感兴趣:)包括我:)
我想创建的是一个小代码,我可以在任何文件中使用,并将生成这样的面包屑:
如果文件名为" website.com/templates/index.php ",则面包屑应显示:
Website.com > Templates
Run Code Online (Sandbox Code Playgroud)
^^链接^^纯文本
如果文件名为" website.com/templates/template_some_name.php ",则面包屑应显示:
Website.com > Templates > Template Some Name
Run Code Online (Sandbox Code Playgroud)
^^链接^^链接^^纯文本
Dom*_*nes 44
对于一个简单的面包屑来说这可能有点过头了,但值得一试.我记得很久以前我刚开始这个问题,但我从未真正解决过这个问题.也就是说,直到我决定现在写这个.:)
我记录了最好的内联,最底层是3个可能的用例.请享用!(随时问你可能有任何问题)
<?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>
Run Code Online (Sandbox Code Playgroud)
Sam*_*152 28
嗯,从你给它的例子看起来像"$ _SERVER ['REQUEST_URI']"和explode()函数可以帮助你.您可以使用explode将域名后面的URL拆分为数组,并在每个正斜杠上将其分开.
作为一个非常基本的例子,可以实现这样的事情:
$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
foreach($crumbs as $crumb){
echo ucfirst(str_replace(array(".php","_"),array(""," "),$crumb) . ' ');
}
Run Code Online (Sandbox Code Playgroud)
小智 8
还使用RDFa创建了一个小脚本(您也可以使用微数据或其他格式)在谷歌上查看 此脚本还会记住您的站点结构.
function breadcrumbs($text = 'You are here: ', $sep = ' » ', $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;}
Run Code Online (Sandbox Code Playgroud)