There is a built-in method in PHP for parsing such a line: '/path/to/../../up/something.txt'

So to speak, I have a line like path

$path = '/path/to/../../up/something.txt';

Is there a way built into PHP for parsing it and creating a URL without loading directories (../)?

eg.

$path = parsePath('/path/to/../../up/something.txt'); // /up/something.txt
+3
source share
2 answers
realpath($path);

Documents

+6
source

PHP is realpath()cool, but what if you want to understand this without access to the file system?

I wrote this function, which can return a path with ../, etc. designed for a real path.

It probably doesn't process all the path commands, so let me know if you think I should implement another.

public function resolvePath($path) {

    while (strstr($path, '../')) {
        $path = preg_replace('/\w+\/\.\.\//', '', $path);
    }

    return $path;

}

, .

0

Source: https://habr.com/ru/post/1734498/


All Articles