List Info

Thread: note 66876 added to function.pathinfo




note 66876 added to function.pathinfo
user name
2006-05-31 10:25:51
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 -- ht
tp://www.php.net/manual/en/function.pathinfo.php
Edit        -- http://master.p
hp.net/note/edit/66876
Del: integrated  -- ht
tp://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://m
aster.php.net/note/delete/66876/spam
Del: non-english -- h
ttp://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://ma
ster.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php

note 66876 deleted from function.pathinfo by bjori
user name
2006-05-31 13:09:39
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

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )