method parsePath documentation in bbn\Str

Replaces backslash with slash in a path string. Forbids the use of ../

Example

X::dump(\bbn\Str::parsePath('\home\user\Desktop')); // (string) "/home/user/Desktop" X::dump(\bbn\Str::parsePath('..\home\user\Desktop')); // (string) "" X::dump(\bbn\Str::parsePath('..\home\user\Desktop', true)); // (string) "home/user/Desktop" function(string $path, $allow_parent = false) { $path = str_replace('\\', '/', \strval($path)); $path = str_replace('/./', '/', \strval($path)); while (strpos($path, '//') !== false){ $path = str_replace('//', '/', $path); } if (strpos($path, '../') !== false) { if (!$allow_parent) { return ''; } $bits = array_reverse(explode('/', $path)); $path = ''; $num_parent = 0; foreach ($bits as $i => $b){ if ($b === '..') { $num_parent++; } elseif ($b !== '.') { if ($num_parent) { $num_parent--; } else{ $path = empty($path) ? $b : $b.'/'.$path; } } } } return $path; }

Replaces backslash with slash in a path string. Forbids the use of ../ BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way

This website uses cookies to ensure you get the best experience on our website.