PHP Breadcrumb Trail script

I’ve just written a neat little script that can be included on every page of your site to automatically create a “breadcrumb trail”-style navigation tool (as used by SitePoint and most other forum software).

It works best if you have a sensible approach to site architecture - so each site area has its own folder, and is named logically, using underscores where necessary (so your music folder would be called “music”, and within that the Barry Manilow page would be called “barry_manilow.php”). :o :wink:

I’d appreciate it if any hardened PHP-ers could cast an eye over it and suggest any improvements, particularly with all the ‘for’ statements I’ve used, as I’m sure there must be a better way of doing those loops.

<p>You are here: <?

$path = $_SERVER["PHP_SELF"];
$tree = split("/",$path);
$breadcrumb = "";
if (count($tree)==2 && $tree[1]=="index.php") { // First, check if we are on the home page
	$breadcrumb = "home";
} else { // If not, then first put a link to the homepage
	$breadcrumb = "<a href=\\"/index.php\\">home</a>";
	for ($i=1; $i<count($tree); $i++) { // Now step through each level adding a link, until we reach an actual file
		if (strstr($tree[$i],".")) { // Found a file (i.e. it has a fullstop character in it)
			if ($tree[$i]!="index.php") { // If it is not the index page of the current folder, print the name
				$pagename = split("\\.",$tree[$i]);
				$breadcrumb = $breadcrumb . " > " . str_replace("_"," ",$pagename[0]);
		} else { // Found another directory, so provide a link to the top level...
			if ($tree[$i+1]=="index.php") { // ...unless the next one down is the index page
				$breadcrumb = $breadcrumb . " > " . str_replace("_"," ",$tree[$i]);
			} else {
				$breadcrumb = $breadcrumb . " > <a href=\\""; // Add the arrow between nodes
				for ($j=1; $j<=$i; $j++) { // Add the right link depth to the actual link
					$breadcrumb = $breadcrumb . "/" . $tree[$j];
				$breadcrumb = $breadcrumb . "/\\">" . $tree[$i] . "</a>";
echo $breadcrumb; // Print the final breadcrumb trail to the page

If anyone else wants to use the script on their own site, feel free. :slight_smile: I recommend using the CSS text-transform property to control the look/capitalization of the output.

Looks fine to me! I rewrote it from scratch, just for fun but the code is about the same length and the approach looks the same:

$path = $_SERVER["PHP_SELF"];
$parts = explode('/',$path);
if (count($parts) < 2)
echo ("<a href=\\"/index.php\\">home</a> -> ");
for ($i = 1; $i < count($parts); $i++)
    if (!strstr($parts[$i],"."))
        echo("<a href=\\"");
		for ($j = 0; $j <= $i; $j++) {echo $parts[$j]."/";};
		echo("\\">".$parts[$i]."</a> ->");
		$str = $parts[$i];
		$pos = strrpos($str,".");
        $parts[$i] = substr($str, 0, $pos);
		echo $parts[$i];


In terms of server load/speed, is your approach of splitting the filename (strpos then substr) better than mine (split then parts[0])?

great script - very easy to implement. One small question…for pages that are named differently than what you’d like them displayed, is there a small command that you can place in the file page that calls the name as you’d like it to appear…for example:

if you had a file in you “company” folder named “sendmailform.php”, but you’d like that file to appear as “Contact Form”, could this be inserted into the script somewhere? Here’s visually what I’m trying to get:

home >> Company >> Contact Form instead of,
home >> Company >> sendmailform

I’m a newbie, and saw this written in another breadcrumb script, and it appeared to be only a few lines of code for the script, and then a line or two of code for each page (on the pages themselves) for the file you’d like to rename.

thanks again for the work!

I guess you would need to set up an associative array of all the filenames that weren’t called something sensible (like your example), and check through that when echoing the filename to the page.

Either that, or set the page name variable in the relevant page explicitly - although I would argue if you’re going to do that, you might as well just print it out using HTML.