Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

PHP – Create Dynamic Breadcrumb

Posted on December 21, 2015February 12, 2016 By David Kittell
<?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>");

    // 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);
}

?>

<?php
// Default options - Home » Page Title
echo breadcrumbs() ;
// Change » to >
// echo breadcrumbs(' > '); 
// Change 'Home' to 'Index' and » to ^^
// echo breadcrumbs(' ^^ ', 'Index');
?>

GitHub: https://gist.github.com/dkittell/85dbbf25e41a30cb5073

Credit goes to Dominic Barnes – http://stackoverflow.com/users/188702/dominic-barnes
http://stackoverflow.com/questions/2594211/php-simple-dynamic-breadcrumb

Originally Posted on December 21, 2015
Last Updated on February 12, 2016
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code PHP

Post navigation

Previous post
Next post

Related Posts

Ektron Content Folder Names

Posted on February 4, 2014October 26, 2015

SELECT c.[content_id] ,c.[content_title] ,c.[content_html] ,c.[content_status] ,CASE WHEN c.[content_status] = ‘A’ THEN ‘Approved’ WHEN c.[content_status] = ‘O’ THEN ‘Checked Out’ WHEN c.[content_status] = ‘I’ THEN ‘Checked In’ WHEN c.[content_status] = ‘S’ THEN ‘Submitted for Approval’ WHEN c.[content_status] = ‘M’ THEN ‘Marked for Deletion’ WHEN c.[content_status] = ‘P’ THEN ‘Pending Go Live…

Read More

UNIX Terminal – Clear Bash History

Posted on October 28, 2015

history -cw All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept…

Read More

Ektron – Remove Content/Folder Permissions

Posted on August 20, 2014October 26, 2015

Use EXTREME caution on this query. I recommend you do a backup of your database before you run this script as this will remove all permissions that someone has setup on Ektron. TRUNCATE TABLE [permissions_tbl]; UPDATE [content] SET [inherit_permissions] = 0 ,[inherit_permissions_from] = 0 ,[private] = 0; Originally Posted on…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes