Skip to content

Instantly share code, notes, and snippets.

@RaffaeleSgarro
Last active August 29, 2015 13:56
Show Gist options
  • Save RaffaeleSgarro/8902552 to your computer and use it in GitHub Desktop.
Save RaffaeleSgarro/8902552 to your computer and use it in GitHub Desktop.
Simple breadcrumb for PHP scripts apps deployed by mapping request paths to the server's filesystem
RewriteEngine On
RewriteRule index.php index.php [END]
RewriteRule (.*) index.php?request=$1 [END]
<?php
// This file is UTF-8 encoded
define('BASE_URL', '/breadcrumbs');
function parse($script, $directorySeparator, $skip) {
mb_regex_encoding('UTF-8');
mb_internal_encoding('UTF-8');
$offset = mb_strlen($skip, 'UTF-8');
$length = mb_strlen($script, 'UTF-8') - $offset;
$script = mb_substr($script, $offset, $length, 'UTF-8');
$directorySeparator = preg_quote($directorySeparator);
return mb_split($directorySeparator, $script);
}
function makeHref($crumbs, $idx, $urlBase) {
$dst = array();
foreach (array_slice($crumbs, 0, $idx + 1) as $crumb) {
$dst []= rawurlencode($crumb);
}
return $urlBase . '/' . implode('/', $dst);
}
function makeHtmlWidget($crumbs, $urlBase = '', $separator = '::') {
$out = '';
// WARNING: use an appropriate function here, even if XML special characters
// are invalid in file names of most filesystems out there
$sanitizeHtml = function($in) { return $in; };
$caption = function($in) { return $in; };
$current_separator = '';
foreach ($crumbs as $idx => $crumb) {
$href = makeHref($crumbs, $idx, $urlBase);
$displayLink = $caption($crumb);
$out .= $current_separator . "<a href='$href'>$displayLink</a>";
$current_separator = $separator;
}
return $out;
}
header("Content-Type: text/html; charset=utf-8");
function test($in, $separator, $skip, $base) {
$crumbs = parse($in, $separator, $skip);
echo '<li>' . makeHtmlWidget($crumbs, $base) . '</li>';
}
echo "<ul>";
test('/var/www/app/TR/Pròdùçts/Tàrgét.php', '/', '/var/www/app/', BASE_URL);
test('C:\\xampp\\htdocs\\app\\TR\\Pròduçts\\Tàrgét.php', '\\', 'C:\\xampp\\htdocs\\app\\', BASE_URL);
echo "</ul>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment