note 124254 added to function.realpath
| From: | moreau dot marc dot web at gmail dot com | Date: | Tue, 01 Oct 2019 07:13:37 +0000 |
| Subject: | note 124254 added to function.realpath | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-212444@lists.php.net to get a copy of this message | ||
<?php
namespace MockingMagician\Organic\Helper;
class Path
{
/**
* There is a method that deal with Sven Arduwie proposal https://www.php.net/manual/en/function.realpath.php#84012
* And runeimp at gmail dot com proposal https://www.php.net/manual/en/function.realpath.php#112367
* @param string $path
* @return string
*/
public static function getAbsolute(string $path): string
{
// Cleaning path regarding OS
$path = mb_ereg_replace('\\\\|/', DIRECTORY_SEPARATOR, $path, 'msr');
// Check if path start with a separator (UNIX)
$startWithSeparator = $path[0] === DIRECTORY_SEPARATOR;
// Check if start with drive letter
preg_match('/^[a-z]:/', $path, $matches);
$startWithLetterDir = isset($matches[0]) ? $matches[0] : false;
// Get and filter empty sub paths
$subPaths = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'mb_strlen');
$absolutes = [];
foreach ($subPaths as $subPath) {
if ('.' === $subPath) {
continue;
}
// if $startWithSeparator is false
// and $startWithLetterDir
// and (absolutes is empty or all previous values are ..)
// save absolute cause that's a relative and we can't deal with that and just
forget that we want go up
if ('..' === $subPath
&& !$startWithSeparator
&& !$startWithLetterDir
&& empty(array_filter($absolutes, function ($value) { return
!('..' === $value); }))
) {
$absolutes[] = $subPath;
continue;
}
if ('..' === $subPath) {
array_pop($absolutes);
continue;
}
$absolutes[] = $subPath;
}
return
(($startWithSeparator ? DIRECTORY_SEPARATOR : $startWithLetterDir) ?
$startWithLetterDir.DIRECTORY_SEPARATOR : ''
).implode(DIRECTORY_SEPARATOR, $absolutes);
}
/**
* Examples
*
* echo Path::getAbsolute('/one/two/../two/./three/../../two'); =>
/one/two
* echo Path::getAbsolute('../one/two/../two/./three/../../two'); =>
../one/two
* echo Path::getAbsolute('../.././../one/two/../two/./three/../../two'); =>
../../../one/two
* echo Path::getAbsolute('../././../one/two/../two/./three/../../two'); =>
../../one/two
* echo Path::getAbsolute('/../one/two/../two/./three/../../two'); =>
/one/two
* echo Path::getAbsolute('/../../one/two/../two/./three/../../two'); =>
/one/two
* echo Path::getAbsolute('c:\.\..\one\two\..\two\.\three\..\..\two'); =>
c:/one/two
*
*/
}
----
Server IP: 208.43.231.9
Probable Submitter: 185.85.3.195 (proxied: 5.51.119.226)
----
Manual Page -- http://php.net/manual/en/function.realpath.php
Edit -- https://master.php.net/note/edit/124254
Del: integrated -- https://master.php.net/note/delete/124254/integrated
Del: useless -- https://master.php.net/note/delete/124254/useless
Del: bad code -- https://master.php.net/note/delete/124254/bad+code
Del: spam -- https://master.php.net/note/delete/124254/spam
Del: non-english -- https://master.php.net/note/delete/124254/non-english
Del: in docs -- https://master.php.net/note/delete/124254/in+docs
Del: other reasons-- https://master.php.net/note/delete/124254
Reject -- https://master.php.net/note/reject/124254
Search -- https://master.php.net/manage/user-notes.php