HELP
HELP

Reputation: 14575

Grab the last value in a URL using PHP

I was wondering how can I grab the last value in a url for example how can I grab the value someone in the url below using PHP. Can someone help me do this?

http://www.example.com/questions/ask/someone

Upvotes: 0

Views: 761

Answers (2)

Abhi
Abhi

Reputation: 5561

search the position of Last slash then make a new substring from the index position of slash to end.It's as simple.

Upvotes: -1

Ben
Ben

Reputation: 57207

Not too hard:

echo basename($url);

As a side note, you can also use dirname to get the remainder of the content.

$url = "http://www.example.com/questions/ask/someone";
echo basename($url);  // someone
echo dirname($url);   // http://www.example.com/questions/ask

Upvotes: 5

Related Questions