在PHP中返回当前url的子文件夹

时间:2011-03-26 01:22:36

标签: php url string

我想:

$path = dirname('http://www.domain.com/folder1/folder2/folder3/folder4');

echo $path;

要返回:

的folder1 /文件夹2 / folder3 / folder4

我希望它能与URL的所有可能替代方案一起使用,例如:

http://www.domain.com/folder1/folder2/folder3/folder4

http://www.domain.com/folder1/folder2/folder3/folder4/

http://folder1.domain.com/folder2/folder3/folder4/index.html

http://www.domain.com/folder1/folder2/folder3/folder4/index.html

http://domain.com/folder1/folder2/folder3/folder4/index.html

我如何在PHP中执行此操作

1 个答案:

答案 0 :(得分:2)

使用parse_url将是最简单的:

parse_url($url, PHP_URL_PATH)

这样做之后你将拥有这条路。但是,如果存在类似index.html的实际文件,则需要检测并删除该文件。