note 57828 added to ref.filesystem
| From: | wsuttonjr at hyponiqs dot com | Date: | Sat, 15 Oct 2005 07:26:01 +0000 |
| Subject: | note 57828 added to ref.filesystem | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-97054@lists.php.net to get a copy of this message | ||
Here's a little function I wrote that I thought someone might find useful. Ever notice those
fancy breadcrumbed headings on Web sites such as Macromedia.com? Ever say, "I want one, but I
don't want to write the HTML in each time?" Well, here you go. This does that based on
your directory structure.
Do note, however, that it only works if you use files and folders that are named like:
/my-folder/my-file.php
/my_folder/my_file.php
/My_Folder/My_File.php
/My-Folder/My-File.php
...and the variable <i>$path</i> must be similarly formatted. A preceding forward slash
(/) must be on that <i>$path</i>. $_SERVER['PHP_SELF'],
$_SERVER['DOCUMENT_ROOT'], and other $_SERVER variables always add that.
When I have some free time (sooner rather than later), I'm going to write a more in-depth
script for this. I just needed something simple for now. You can expect a PHP 4 and PHP 5 script
class dedicated to this one idea. For now, this simple function does the trick. Anyway, here you
go:
<?php
function breadCrumbs($path)
{
/**
* What it does is split a path string into its two base objects -- a
* file name and a directory name.
*
* After that, it then splits the directory into an array of sub-
* directories and adds them to a stack of links with keys of 'name'
* (directory name) and 'href' (hyperlink reference path).
*
* Each item in this indexed associative array is then stacked again
* into an array of actual hyperlinks (i.e. <a href="Blah/Blah2.php">)
* split by a tree notification ( > ) of sorts.
*
* Finally, the entire string, which may look like:
* <a href="#">Services</a> > <a href="#">Web</a>
* has the current file being viewed added to it. That finished string
* is then returned to the calling script/function for display to the
* browser.
*
* The returned HTML may look like:
* <a href="#">Services</a> > <a href="#">Web</a>
> Home
*/
// set the index file and name for each directory
$dirIndex = 'Home.php';
$dirIndexName = 'Home';
// split $path into basename and dirname
$file = basename($path, '.php');
$dir = dirname($path);
// change all backslashes to forward slashes
$dir = str_replace('\\', '/', $dir);
// remove preceding forward slash (/)
$dir = substr($dir, 1, strlen($dir));
// ** ADDED **
// add a trailing '/ ' to add a blank folder name - will be removed
// NOTE: This is to ensure that an array IS created
$dir .= '/ ';
// split dirname into an array
$dirs = preg_split('@\/@', $dir);
// get the last array index from $dirs
$lastIndex = count($dirs) - 1;
// remove last $dirs if empty
if ($dirs[$lastIndex] == '' || is_null($dirs[$lastIndex])) {
unset($dirs[$lastIndex]);
}
// remove empty dir
if ($dirs[$lastIndex] == ' ') {
unset($dirs[$lastIndex]);
}
// get an accurate directory count
$dirCount = count($dirs);
// create $link and $links variables
$link = '/';
$links = array();
// stack each directory into a link and dirname
for ($i = 0; $i < count($dirs); $i++) {
// change $dirs[$i] into capitalized word(s) and add spaces (for
// grammatical correctness)
$link .= ucwords(preg_replace('@(-|_)@', ' ', $dirs[$i])) . '/';
$links[$i]['href'] = $link . $dirIndex;
$links[$i]['name'] = ucwords($dirs[$i]);
}
// instanciate $breadcrumbs array
$breadcrumbs = array();
// stack $links into HTML-equivalent links
for ($i = 0; $i < count($links); $i++) {
$breadcrumbs[$i] = '<a href="' . $links[$i]['href'] .
'">' . $links[$i]['name'] . '</a>';
}
// see if links are needed or if a parent document
if ($dirs[0] == '' || empty($dirs[0])) {
// it is not so use just the file name
$breadcrumb = $file;
} else {
// it is so compile the links into a breadcrumb string
$breadcrumb = '<a href="/' . $dirIndex . '">' . $dirIndexName
. '</a> — ' . join($breadcrumbs, ' > ');
$breadcrumb .= ' > ' . $file;
}
// return compiled breadcrumb string
return $breadcrumb;
}
?>
----
Manual Page -- http://www.php.net/manual/en/ref.filesystem.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57828
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57828&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57828&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57828&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57828&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57828&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57828&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57828&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57828&report=yes
Search -- http://master.php.net/manage/user-notes.php