开发者

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开发者_StackOverflow社区 of the current url.


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

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


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

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


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


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜