Skip to content

Instantly share code, notes, and snippets.

@dkittell
Last active July 21, 2022 18:59
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dkittell/85dbbf25e41a30cb5073 to your computer and use it in GitHub Desktop.
Save dkittell/85dbbf25e41a30cb5073 to your computer and use it in GitHub Desktop.
PHP - Dynamic Breadcrumbs
<?php
// Credit goes to Dominic Barnes - http://stackoverflow.com/users/188702/dominic-barnes
// http://stackoverflow.com/questions/2594211/php-simple-dynamic-breadcrumb
// This function will take $_SERVER['REQUEST_URI'] and build a breadcrumb based on the user's current path
function breadcrumbs($separator = ' &raquo; ', $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>");
// Initialize crumbs to track path for proper link
$crumbs = '';
// 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', '_', '%20'), array('', ' ', ' '), $crumb));
// If we are not on the last index, then display an <a> tag
if ($x != $last) {
$breadcrumbs[] = "<a href=\"$base$crumbs$crumb\">$title</a>";
$crumbs .= $crumb . '/';
}
// Otherwise, just display the title (minus)
else {
$breadcrumbs[] = $title;
}
}
// Build our temporary array (pieces of bread) into one big string :)
return implode($separator, $breadcrumbs);
}
?>
<?php
// Default options - Home » Page Title
// echo breadcrumbs();
// Change » to >
echo breadcrumbs(' > ');
// Change 'Home' to 'Index' and » to ^^
// echo breadcrumbs(' ^^ ', 'Index');
?>
@yachtkeel
Copy link

Hello,
great script man; just have a quick question,

the script is giving all parent directories but not children
example:
example.com/talent/
example.com/talent/celebrity

so in the breadcrumb it shows:
example.com/talent/ ( which is right)
example.com/celebrity (which is wrong) => should be example.com/talent/celebrity

any chance you can guide me to solve this issue?

thanks bro anyways

@pilinux
Copy link

pilinux commented Jun 28, 2017

Hi @yachtkeel, I just stumbled upon this gist. You may try this one:

```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 == 1 && $x != $last){
        $breadcrumbs[] = "<a href=\"$base$crumb\">$title</a>";
		$breadcrumbs0 = "$base$crumb/";
	}
	
	elseif ($x == 2 && $x != $last)
        $breadcrumbs[] = "<a href=\"$breadcrumbs0$crumb\">$title</a>";

    // Otherwise, just display the title (minus)
    else
        $breadcrumbs[] = $title;
} ```

It is not the best solution but it will work for example.com/talent/celebrity/name. If you need more steps, just add more elseif conditions unless you want to rewrite the whole function to make it dynamic for infinitely many steps.

@random-peon-00
Copy link

random-peon-00 commented Aug 26, 2020

I ran into this and came up with a fix by adding a crumbs (note plural) to add the crumb with a '/'. only 2 lines of code needed:


/*
Credit goes to Dominic Barnes - http://stackoverflow.com/users/188702/dominic-barnes
http://stackoverflow.com/questions/2594211/php-simple-dynamic-breadcrumb
2020-08-26 RAS update to track crumbs for propery link formation and to swap %20 for space
*/
// This function will take $_SERVER['REQUEST_URI'] and build a breadcrumb based on the user's current path
function breadcrumbs($separator = ' &raquo; ', $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>");

    // Initialize crumbs to track path for proper link
    $crumbs='';

    // 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', '_', '%20'), Array('', ' ', ' '), $crumb));

        // If we are not on the last index, then display an <a> tag
        if ($x != $last) {
            $breadcrumbs[] = "<a href=\"$base$crumbs$crumb\">$title</a>";
    	    $crumbs .= $crumb.'/';
        }
        // Otherwise, just display the title (minus)
        else
            $breadcrumbs[] = $title;
    }

    // Build our temporary array (pieces of bread) into one big string :)
    return implode($separator, $breadcrumbs);
}

?>`

@dkittell
Copy link
Author

It appears that my email notifications finally started working again....

Thank you @random-peon-00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment