Hakan
Hakan

Reputation: 3885

Return subfolders of current url in PHP

I want:

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

echo $path;

To return:

folder1/folder2/folder3/folder4

I want it to work with all possible alternatives of the URL eg.:

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

How do I do that in PHP

Upvotes: 1

Views: 1309

Answers (1)

prodigitalson
prodigitalson

Reputation: 60403

Using parse_url would be the easiest:

parse_url($url, PHP_URL_PATH)

After doing that you will have the path. However if there is an actual file like index.html then youll need to detect and remove that.

Upvotes: 2

Related Questions