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
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|