Skip to content

Instantly share code, notes, and snippets.

@RahulSaini91
Last active March 14, 2019 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RahulSaini91/89651992e3f2f54f6e2ed1cf207d2c2c to your computer and use it in GitHub Desktop.
Save RahulSaini91/89651992e3f2f54f6e2ed1cf207d2c2c to your computer and use it in GitHub Desktop.
function url2breadcrumb($url,$include_base=1,$home_url=false){
if(empty($url)){
return;
}
$base = parse_url($url);
$base = $base['scheme'].'://'.$base['host'].'/';
$crumbs = rtrim(parse_url($url,PHP_URL_PATH),'_');
$crumbs = array_filter(explode("/",$crumbs));
$link = '';
$breadcrumbs = array();
$exclude = array('.htm','.html','.php'); //will be deleted from crumb text
$replace = array('-','_'); //will be replaced to whitespace
foreach($crumbs as $crumb){
//exclude home url
if($home_url){
$h = str_replace($base,'',$home_url);
$h = ltrim($h,'/');
$h = rtrim($h,'/');
if($crumb == $h){
continue;
}
}
$link .= $crumb.'/';
$text = str_replace($replace,' ',$crumb);
$the_crumb = array(
'text'=>ucwords(str_replace($exclude,'',$text)),
'link'=>($include_base==1?$base:'').rtrim($link,"/")
);
$breadcrumbs[] = $the_crumb;
}
return $breadcrumbs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment