Checking for relative vs absolute paths/URLs in PHP

前端 未结 7 661
既然无缘
既然无缘 2021-01-17 15:45

I need to implement functions to check whether paths and urls are relative, absolute, or invalid (invalid syntactically- not whether resource exists). What are the range of

7条回答
  •  青春惊慌失措
    2021-01-17 16:24

    From Symfony FileSystem component to check if a path is absolute:

    public function isAbsolutePath($file)
    {
        return strspn($file, '/\\', 0, 1)
            || (strlen($file) > 3 && ctype_alpha($file[0])
                && substr($file, 1, 1) === ':'
                && strspn($file, '/\\', 2, 1)
            )
            || null !== parse_url($file, PHP_URL_SCHEME)
        ;
    }
    

提交回复
热议问题