note 66876 deleted from function.pathinfo by bjori
| From: | bjori@php.net | Date: | Wed, 31 May 2006 13:09:39 +0000 |
| Subject: | note 66876 deleted from function.pathinfo by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-112638@lists.php.net to get a copy of this message | ||
Note Submitter: julien_garand at hotmail dot com
Reason: useless
----
Hi,
I make this tiny function, which uses pathinfo() to explode a path string into an array...
<?php
/*array*/ function explode_path( /*string*/ $path ) {
if( !is_string($path) or empty($path) ) {
trigger_error('explode_path() require a not-empty string as first parameter');
return array();
}
$path = array($path);
while( ($path[0] != '.') and ($path[0] != '..') and ($path[0] != '/')
) {
$pathinfo = pathinfo( array_shift( $path ) );
if( $pathinfo['dirname'] == '/' ) {
array_unshift( $path, '/'.$pathinfo['basename'] );
return $path;
}
array_unshift( $path, $pathinfo['dirname'], $pathinfo['basename'] );
}
return $path;
}
?>
some examples of uses :
<?php
print_r( explode_path('one/two/file.ext') );
// Array
// (
// [0] => .
// [1] => one
// [2] => two
// [3] => file.ext
// )
print_r( explode_path('/one/two/file.ext') );
// Array
// (
// [0] => /one
// [1] => two
// [2] => file.ext
// )
print_r( explode_path('../one/two/file.ext') );
// Array
// (
// [0] => ..
// [1] => one
// [2] => two
// [3] => file.ext
// )
?>
Then, rebuilding the path is very simple by using implode('/', $path_array);
I hope you will find it fine...
Julien Garand