Rost
Rost

Reputation: 185

Get basename in url

I have url

http://localhost:8888/cr/organization/test

How i can get organization from URL?

I'm using

basename($_SERVER['REQUEST_URI']); But it returns just test;

Any way for me to get organization?

Upvotes: 0

Views: 1786

Answers (1)

Joseph_J
Joseph_J

Reputation: 3669

You can use pathinfo() to obtain an array that breaks down the url into different components. One of the components is the directory name. Use explode() with the / as the delimiter and that will output an array containing all of the folders in the url.

Like so:

$path_parts = pathinfo('http://localhost:8888/cr/organization2/test2');

$folders = explode('/', $path_parts['dirname']);

print_r($folders);

Outputs:

Array
(
    [0] => http:
    [1] => 
    [2] => localhost:8888
    [3] => cr
    [4] => organization
)

As I mentioned earlier pathinfo() provides an array with different components in it. Another component is the basename. That will provide you with the "test2" in your example.

echo $path_parts['basename']; //Outputs "test2"

Hopefully that will get you started to where you can apply some logic to get the desired results.

Upvotes: 2

Related Questions