0

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
HELP
  • 14,237
  • 22
  • 66
  • 100

2 Answers2

5

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
Ben
  • 54,723
  • 49
  • 178
  • 224
  • but what if I dont now the url? – HELP Dec 28 '10 at 08:16
  • Uhhh...then your question becomes invalid. You can use `$_SERVER['PHP_SELF']` to find the current script, check out http://php.net/manual/en/reserved.variables.server.php. – Ben Dec 28 '10 at 08:18
  • why would my question become invalid? – HELP Dec 28 '10 at 08:19
  • "how can I grab the last value in a url" sounds like you have the URL string defined already. Doesn't matter though, you can use the solution in my last comment to find the current URL. – Ben Dec 28 '10 at 08:20
  • Would someone care to explain the downvote? Did my response not answer the question? – Ben Dec 28 '10 at 08:21
  • and your `$_SERVER['PHP_SELF']` does not work at grabing the whole url. – HELP Dec 28 '10 at 08:21
  • Check the link for the variable you want. Research, dude. – Ben Dec 28 '10 at 08:22
  • dude, I didn't ask for a link and you did not specify to what to look for dude. – HELP Dec 28 '10 at 08:24
  • @scfy, Steve absolutely has answered your question. If you have a further question about how to get the current URL of a visited page in PHP, open another question post and ask away. Don't penalize people who provide correct answers just because they can't read your mind. – eyelidlessness Dec 28 '10 at 08:34
  • @eyelidlessness I did not penalize anybody I was just calling it the way I read it. he really did not answer my question because `$_SERVER['PHP_SELF']` would of just sent back a index.php file instead of `someone`. – HELP Dec 28 '10 at 08:38
  • @scfy, the question about how to get the current URL is out of scope of the question you asked. You asked how to get the last segment of a given URL path, which Steve provided. If you want to ask further questions, open further $#%*# questions! – eyelidlessness Dec 28 '10 at 08:40
  • 1
    @scfy, the purpose of this site is to ask and answer narrow, specific questions that can have single correct answers. You are asking two related but different questions with two distinct answers in one (and moreover, the second question is not present in the actual question post). There is every reason to open another question (or, to search for questions that may have already answered your question, which certainly exist, like this one: http://stackoverflow.com/questions/1283327/how-to-get-url-of-current-page-in-php) – eyelidlessness Dec 28 '10 at 09:20
  • @scfy CTFO. Being rude or angry at people who are trying to help you isn't acceptable around here. –  Dec 29 '10 at 20:17
-1

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

Abhi
  • 5,501
  • 17
  • 78
  • 133