onsa
12/17/2016 - 3:55 PM

Create breadcrumbs based on the $_SERVER['REQUEST_URI'] variable.

Create breadcrumbs based on the $_SERVER['REQUEST_URI'] variable.

<?php

	  function breadcrumbs($separator, $home) {
		  
	  // add white space to separator
	  $separator = "&nbsp;&nbsp;&nbsp;$separator;&nbsp;&nbsp;&nbsp;";
	  
	  // get REQUEST_URI (/path/to/file.php), split the string (using '/') into an array, filter out any empty values
	  $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
	  
	  // build base URL, account for HTTPS
	  $base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
	  
	  // initialize temporary array for breadcrumbs
	  $breadcrumbs = Array("<a href=\"$base\">$home</a>");
	  
	  // find out the index for the last value in our path array
	  $keys = array_keys($path);
	  $last = end($keys);
	  
	  // define empty variable to concatenate link reference
	  $cumulativecrumbs='';
	  
	  // build the rest of the breadcrumbs
	  foreach ($path as $x => $crumb) {
		  // create title, i.e. text to be displayed (strip out .php and turn '-'s into spaces)
		  $title = ucwords(str_replace(Array('.php', '-'), Array('', ' '), $crumb));
		  
		  // until the last index, display an <a> tag
		  if ($x != $last) {
			  $cumulativecrumbs = $cumulativecrumbs . $crumb . "/";
			  $breadcrumbs[] = "<a href=\"$base$cumulativecrumbs\">$title</a>";
		  // at the last index, display the title
		  } else {
			  $breadcrumbs[] = "<span ID='lastcrumb'>$title</span>";
		  }
		  }
		  
	  // transform temporary array into one string
	  return implode($separator, $breadcrumbs);
	  }
?>