note 66876 added to function.pathinfo
| From: | julien_garand at hotmail dot com | Date: | Wed, 31 May 2006 10:25:51 +0000 |
| Subject: | note 66876 added to function.pathinfo | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-112628@lists.php.net to get a copy of this message | ||
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
----
Server IP: 217.174.203.10
Probable Submitter: 84.5.218.218
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.pathinfo.php
Edit -- http://master.php.net/note/edit/66876
Del: integrated -- http://master.php.net/note/delete/66876/integrated
Del: useless -- http://master.php.net/note/delete/66876/useless
Del: bad code -- http://master.php.net/note/delete/66876/bad+code
Del: spam -- http://master.php.net/note/delete/66876/spam
Del: non-english -- http://master.php.net/note/delete/66876/non-english
Del: in docs -- http://master.php.net/note/delete/66876/in+docs
Del: other reasons-- http://master.php.net/note/delete/66876
Reject -- http://master.php.net/note/reject/66876
Search -- http://master.php.net/manage/user-notes.php