Skip to content

Instantly share code, notes, and snippets.

@JakeHenshall
Last active August 29, 2015 14:04
Show Gist options
  • Save JakeHenshall/03e5206b888fe667927e to your computer and use it in GitHub Desktop.
Save JakeHenshall/03e5206b888fe667927e to your computer and use it in GitHub Desktop.
Beautiful Breadcrumbs
<?php
function breadcrumbs($separator = ' > ', $home = 'Home') {
$path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
$base_url = substr($_SERVER['SERVER_PROTOCOL'], 0, strpos($_SERVER['SERVER_PROTOCOL'], '/')) . '://' . $_SERVER['HTTP_HOST'] . '/';
$breadcrumbs = array("<a href=\"$base_url\">$home</a>");
$tmp = array_keys($path);
$last = end($tmp);
unset($tmp);
foreach ($path as $x => $crumb) {
$title = ucwords(str_replace(array('.php', '_'), array('', ' '), $crumb));
if ($x == 1){
$breadcrumbs[] = "<a href=\"$base_url$crumb\">$title</a>";
}elseif ($x > 1 && $x < $last){
$tmp = "<a href=\"$base_url";
for($i = 1; $i <= $x; $i++){
$tmp .= $path[$i] . '/';
}
$tmp .= "\">$title</a>";
$breadcrumbs[] = $tmp;
unset($tmp);
}else{
$breadcrumbs[] = "$title";
}
}
return implode($separator, $breadcrumbs);
}
echo breadcrumbs();
?>
To make it work add these 2 lines of code on the html page you want to display them on.
This includes the breadcrumbs php file.
<?php include("../includes/breadcrumbs.php")?>
This displays the breadcrumb function.
<?php echo breadcrumbs(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment