@ 2017-07-19T16:39:56Z <?php
$test = '~/test/../test2/test3/../file.php';
// Fake home setting.
putenv( 'HOME=/Users/alain' );
function get_home_directory() {
// Unix.
$home = getenv( 'HOME' );
if ( ! empty( $home ) ) {
return rtrim( $home, '/' );
}
// Windows.
if ( ! empty( $_SERVER['HOMEDRIVE'] ) && ! empty( $_SERVER['HOMEPATH'] ) ) {
$home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH'];
return rtrim( $home, '\\/' );
}
return '~';
}
function get_normalized_path($path) {
return array_reduce(explode('/', $path), function( $a = '/', $b ) {
switch ( $b ) {
case '':
case '.':
return $a;
case '..':
return dirname( $a );
case '~':
return get_home_directory();
default:
return preg_replace("/\/+/", "/", "$a/$b");
}
} );
}
echo $test . PHP_EOL;
echo get_normalized_path( $test ) . PHP_EOL;
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.1.0 - 8.1.30 , 8.2.0 - 8.2.25 , 8.3.0 - 8.3.13 Deprecated: Optional parameter $a declared before required parameter $b is implicitly treated as a required parameter in /in/9Ga8c on line 23
~/test/../test2/test3/../file.php
/Users/alain/test2/file.php
Output for 8.0.0 - 8.0.30 Deprecated: Required parameter $b follows optional parameter $a in /in/9Ga8c on line 23
~/test/../test2/test3/../file.php
/Users/alain/test2/file.php
Output for 5.6.0 - 5.6.40 , 7.0.0 - 7.0.33 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.34 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 ~/test/../test2/test3/../file.php
/Users/alain/test2/file.php
preferences:dark mode live preview
88.33 ms | 409 KiB | 5 Q