How to get folder name out of a URL?

Can somebody tell me how to get the name of the folder name below?

I would like to make an if statement based on the name of where THIS_ONE is located.

Any help would be greatly appreciated.

in a nutshell: take URI, and do preg_split() for ‘/’…

$path = $_SERVER['REQUEST_URI']; // this gives you /folder1/folder2/THIS_ONE/file.php
$folders = preg_split('/', $path); // splits folders in array
$what_we_need = $folders[3];

hope this helped…

No need for regex

$url = "";
$urlParts = explode("/", $url);
echo $urlParts[5];

Do a


To see all the parts.

You’re not seriously using preg_split when you have such a simple expression, do you? :rolleyes:
Use explode() for splitting a string at a character; it is much faster :slight_smile:

But back on topic:

$url = '';
$parsedUrl = parse_url($url);
echo $parsedUrl['path'];

Alternatively you could use something like dirname()+explode()+array_pop(), but that would not be as elegant as the code above :wink:

ok guys, you got me :smiley:

explode() is faster, no doubt, but in this case I don’t think it makes much of a differece. anyhow - you’re right…

Thanks for the help guys!