Eli
Eli

Reputation: 4359

convert '../../' into 'path/path/'

Is there a way to convert a path that looks like ../../../ into path/path/path, I want to make a search class but got to thinking that anyone could pass in the ../ format instead of path/ and return a list of the files that are found.

I started to approach this by using preg_replace() to find and replace everything up until it matches the $_SERVER['DOCUMENT_ROOT'] in the path being passed. But the only solves the path/path/path problem.

How would I approach the ../../../ problem?

Upvotes: 0

Views: 105

Answers (2)

anubhava
anubhava

Reputation: 784918

Not very clear about the ask but probably this will work for you:

$path = '../../..';
$path = str_replace(realpath($path) . '/', '', getcwd());

Upvotes: 0

Adam
Adam

Reputation: 2334

What about realpath?

http://php.net/manual/en/function.realpath.php

Upvotes: 3

Related Questions