Voting

: max(one, one)?
(Example: nine)

The Note You're Voting On

Sven Arduwie
17 years ago
Because realpath() does not work on files that do not
exist, I wrote a function that does.
It replaces (consecutive) occurences of / and \\ with
whatever is in DIRECTORY_SEPARATOR, and processes /. and /.. fine.
Paths returned by get_absolute_path() contain no
(back)slash at position 0 (beginning of the string) or
position -1 (ending)
<?php
function get_absolute_path($path) {
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
$parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
$absolutes = array();
foreach (
$parts as $part) {
if (
'.' == $part) continue;
if (
'..' == $part) {
array_pop($absolutes);
} else {
$absolutes[] = $part;
}
}
return
implode(DIRECTORY_SEPARATOR, $absolutes);
}
?>

A test:
<?php
var_dump
(get_absolute_path('this/is/../a/./test/.///is'));
?>
Returns: string(14) "this/a/test/is"

As you can so, it also produces Yoda-speak. :)

<< Back to user notes page

To Top