Breadcrumb PHP method


/ Published in: PHP
Save to your folder(s)

This method will use the request)uri to create breadcrumbs

add this where you what crumb to show up echo breadcrumbs();


Copy this code and paste it in your HTML
  1. function breadcrumbs($separator = ' / ', $home = 'Home') {
  2.  
  3. $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
  4. $base_url = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
  5. $breadcrumbs = array("<a href=\"$base_url\">$home</a>");
  6.  
  7. $last = end(array_keys($path));
  8.  
  9. foreach ($path AS $x => $crumb) {
  10. $title = ucwords(str_replace(array('.php', '_'), Array('', ' '), $crumb));
  11. if ($x != $last){
  12. $breadcrumbs[] = '<a href="$base_url$crumb">$title</a>';
  13. }else{
  14. $breadcrumbs[] = $title;
  15. }
  16. }
  17.  
  18. return implode($separator, $breadcrumbs);
  19. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.