I had other requirements and needed a far more flexible solution, a bread crumbs path for a folder 'somewhere' on my web server, which is not within the file structure of my website. Because of this, I can't use most solutions here.
On my Windows machine the path looks like this:
C:\wamp64\www\VIBE\screening_service\public\patients\patient_00005238\screening 20210608_1827
I wanted my breadcrumbs look like this:
public › patients › patient_00005238 › screening 20210608_1827
So, 'public' is the start folder (which I call $baseFolder in my code).
For the displayed text of the thumbs, I can take the URL's which belong to these folders, but I have to leave a part out, (which I will call $baseUrl) in my code. The part I need to leave out:
/VIBE/screening_service/
By later merging $baseUrl '/VIBE/screening_service/' with 'patients/patient_00005238', you can normally create valid links. Not in my case though, because these folders are outside of my website
A web browser will interpret the merged strings
'/VIBE/screening_service/patients/patient_00005238/'
as
'http://localhost:8080//VIBE/screening_service/patients/patient_00005238/'.
In my case I wanted just the crumb path ( like 'patients/patient_00005238' ), which I add to the data attribute of the div of the crumb.
I define a Javascript click event and will use the data attribute to reconstruct the real path I need.
Well all this resulted in this function, which might be useful to somebody.
Call it like this
$folder = 'C:\wamp64\www\VIBE\screening_service\public\patients\patient_00005238\screening 20210608_1827';
$baseFolder = 'C:\wamp64\www\VIBE\screening_service\\';
$baseUrl = '/VIBE/screening_service/';
$breadCrumbs = getBreadCrumbs($folder, $baseFolder, $baseUrl, 'crumbs', ' › ', true);
Note that $baseUrl maps to $baseFolder on the web server.
function getBreadCrumbs(string $folder, string $baseFolder, string $baseUrl, string $cssClassName = '', string $separator = ' › ', bool $returnLinks = true): string
{
// Start working with the file system folder structure
// Remove the base folder part we do not want to be part of our crumbs
$url = str_replace($baseFolder, '', $folder);
// Replace Windows directroy seperators (backward slashes) with forward slashes
$url = str_replace('\\', DIRECTORY_SEPERATOR, $url);
// Get the crumbs array from this URL
$crumbs = array_filter(explode('/', $url));
// How many crumbs have we?
$crumbCount = count($crumbs);
// We use the CSS class name for styling our crumbs, if it is not empty
$cssClass = empty($cssClassName) ? '' : sprintf(' class="%s"', $cssClassName);
// Start building our crumbs trail (a DIV element and add the (optional CSS class)
$trail = sprintf('<div%s>', $cssClass) . PHP_EOL;
// Do we want links? If yes, we have to start them with the $baseUrl
$crumbUrl = $returnLinks ? rtrim($baseUrl, '/') . '/' : '';
// Iterate through the crumbs
foreach($crumbs as $i => $crumb)
{
// We create a separator, except for the last crumb
$sep = $i < $crumbCount-1 ? $separator : '';
// Add the crumb to our crumbs trail
$crumbUrl .= $crumb . '/';
// Do we want links? If yes, create the link part in our HTML
$html = $returnLinks ? sprintf('<a href="%s">%s</a>', $crumbUrl, $crumb) : $crumb;
// Wrap our link in a DIV element and add a data attribute with the crumb URL (which can be used in Javascript)
$html = sprintf('<div data-crumb="%s">%s</div>', $crumbUrl, $html);
// Add the HTML to our crumbs trail
$trail .= $html . $sep . PHP_EOL;
}
// End building our crumbs trail
$trail .= '</div>' . PHP_EOL;
return $trail;
}