LoginSignup
1
2

More than 5 years have passed since last update.

絶対パスに変換する関数

Posted at

realpath に似てるけど、ファイルが無くても絶対パスにする。

/** realpath の非実在系関数
 *
 * ex:
 *    >>> print abs_path("/path/to/dir/../arr/to/best");
 *    "/path/to/arr/to/best"
 *
 * @param $str string 整合性合わせたいパス
 * @result string 整合性合わせたパス 
 */
function abs_path (string $str) {
    $fn = preg_split("|/|", $str);
    $stack = array();
    foreach ($fn as $path) {
        if ($path === "..") {
            if (count($stack))
                array_pop($stack);
        }
        else if ($path === ".") {
            // 無視
        }
        else if ($path === "") {
            // 無視
        }
        else {
            array_push($stack, $path);
        }
    }
    return implode("/", $stack);
}
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2