NaN
NaN

Reputation: 9094

How to remove part of path until a given directory is found?

I have paths that goes like this template:

`/dir1/dir2/dirN/dirNplus1/dirNplus2/`

And for an example to demonstrate this template:

`/var/www/sei/modules/module1/`

I would like to be able to have a function where I could input a full path and a specific directory and get in return only the right part of the path, removing the left part including the directory specified in the parameter.

In the example given, if I would use:

`function('/var/www/sei/modules/module1/', 'sei')`

Then I would like to get the result as:

`/modules/module1/`

Any ideas on how to achieve this?

Upvotes: 0

Views: 96

Answers (1)

NaN
NaN

Reputation: 9094

As @ADyson suggested, I wrote this code below:

const DIRETORIO_SEI  = 'sei';

private static $caminho;

public static function getCaminhoModulo($append = '') {
    if (self::$caminho != null) {
        return self::$caminho;
    }
    $diretorio = explode('/', realpath(__DIR__.'/../../../'));
    $caminho = '';
    $incluir = false;
    for ($i = 0; $i < count($diretorio); $i++) {
        if ($incluir) {
            $caminho = $caminho . $diretorio[$i] . '/';
        }
        if ($diretorio[$i] == self::DIRETORIO_SEI) {
            $incluir = true;
        }
    }
    self::$caminho = $caminho;
    return $caminho.$append;
}

Upvotes: 1

Related Questions