Satch3000
Satch3000

Reputation: 49384

PHP Get last word in URL between / and /

How do I get the last word in a url between / and /

For example:

http://mywebsite.com/extractMe/

I want to get "extractMe" (without the quotes) from the url

NOTE: I need to get the "extractMe" equivilent of the current url.

Upvotes: 0

Views: 1814

Answers (4)

Billy Moon
Billy Moon

Reputation: 58531

With regex you can match this pattern, or many other more intricate patterns:

echo preg_replace('/.*\/(.+?)\/$/','$1',$url);

Upvotes: 1

Felix Kling
Felix Kling

Reputation: 816404

In case you want to get the full path, you can use parse_url:

$path = trim(parse_url($url, PHP_URL_PATH), '/');

Upvotes: 0

John Cartwright
John Cartwright

Reputation: 5084

I prefer to use basename() in these circumstances as it's more implicit.

echo basename('http://mywebsite.com/extractMe/'); //extractMe

Upvotes: 8

dynamic
dynamic

Reputation: 48091

$e=explode('/',$url);
echo $e[count($e)-1];

Upvotes: 0

Related Questions