<?php
$url = 'www.example.com/title/page/article/'; //this is the URL
$urlArray = explode('/', $url); //Strip down the URL
$startURL = $urlArray[0]; //we want to keep this for if the user clicks on start.
$urlArray[0] = 'Start'; //replace www.example.com with "start"
if($urlArray[count($urlArray) - 1] == ''){ //this checks the last item is being empty or not.
array_pop($urlArray); //last item is indeed empty so we just remove it.
}
$broadCrumb = ''; //create an empty broadCrumb variable
Foreach($urlArray as $index =>$value){ //loop through the stripped down url
$value = ucfirst($value); //initcap the items
$broadCrumbTemp = '<a href="'.$startURL.'/{url}">{linkname}</a>{delimeter}'; //this is the template we are going to use.
if($index == (count($urlArray) - 1)){ //if the index is last
$broadCrumb .= $value; //just append the item
}else{ //if the index isn't last
$location = '';
for($i = 1; $i <= $index; $i++){
$location .= $urlArray[$i].'/';
}
$replace = array('{url}', '{linkname}', '{delimeter}');
$replaceTo = array($location, $value, ' > ');
$broadCrumbTemp = str_ireplace($replace, $replaceTo, $broadCrumbTemp);
$broadCrumb .= $broadCrumbTemp;
}
}
echo($broadCrumb); //show the broadCrumb