开发者

PHP Get last word in URL between / and /

开发者 https://www.devze.com 2023-03-13 09:37 出处:网络
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

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

精彩评论

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

关注公众号