note 100421 added to function.pathinfo
| From: | fabiolimasouto at gmail dot com | Date: | Thu, 14 Oct 2010 19:53:05 +0000 |
| Subject: | note 100421 added to function.pathinfo | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-173229@lists.php.net to get a copy of this message | ||
Sometimes we want to get an array of each component in the pathname associated with the
correspondent path to that directory. We need it for creating breadCrumbs and stuff like that. So
here it goes:
<?php
/* Function parsePathComponents
* parse through all path components
* resolves the cross platform slash issue
* eliminates extra redundant slashes
* @return associative array: ComponentName=>CorrespondentPath
*/
/* Array */ function parsePathComponents($path,$endSlash=true,$base=false)
{
for(
$path = trim($path),
$slash = strstr(PHP_OS,'WIN') ? '\/' : '/',
$retArray = array(),
$str = $temp = "",
$x = 0;
$char = @$path{$x}; $x++)
{
if(!strstr($slash,$char)) $temp .= $char;
elseif($temp){
$str .= $temp;
$retArray[$temp] = $str.($endSlash ? $slash{0} : '');
$str .= $slash{0};
$temp = "";
}
}
$base&&$temp and $retArray[$temp] = $str.$temp;
return $retArray;
}
// Testing in WINDOWS
// NOTE: UNIX will return paths with normal slashes
// return array with path names ending with a slash
$path = '/my//stupid//path/to///some/file.php';
$pathComps = parsePathComponents($path);
print_r($pathComps);
//Array ( [my] => my\ [stupid] => my\stupid\ [path] => my\stupid\path\ [to] =>
my\stupid\path\to\ [some] => my\stupid\path\to\some\ )
$path = 'my/other//path/';
// return paths not ending with slash
print_r(parsePathComponents($path,false));
// Array ( [my] => my [other] => my\other [path] => my\other\path )
$path = 'my//other/path/to///file.php';
// use third argument = true: for capturing the "file.php" base
print_r(parsePathComponents($path,true,true));
//Array ( [my] => my\ [other] => my\other\ [path] => my\other\path\ [to] =>
my\other\path\to\ [file.php] => my\other\path\to\file.php )
?>
----
Server IP: 69.147.83.197
Probable Submitter: 201.34.81.231
----
Manual Page -- http://www.php.net/manual/en/function.pathinfo.php
Edit -- https://master.php.net/note/edit/100421
Del: integrated -- https://master.php.net/note/delete/100421/integrated
Del: useless -- https://master.php.net/note/delete/100421/useless
Del: bad code -- https://master.php.net/note/delete/100421/bad+code
Del: spam -- https://master.php.net/note/delete/100421/spam
Del: non-english -- https://master.php.net/note/delete/100421/non-english
Del: in docs -- https://master.php.net/note/delete/100421/in+docs
Del: other reasons-- https://master.php.net/note/delete/100421
Reject -- https://master.php.net/note/reject/100421
Search -- https://master.php.net/manage/user-notes.php