在 PHP 中的斜杠后从 URL 中获取最后一个单词

2022-01-04 00:00:00 url get php

我需要从 URL 中获取最后一个词.例如,我有以下网址:

I need to get the very last word from an URL. So for example I have the following URL:

http://www.mydomainname.com/m/groups/view/test

我只需要使用 PHP 进行测试",别无其他.我试图使用这样的东西:

I need to get with PHP only "test", nothing else. I tried to use something like this:

$words = explode(' ', $_SERVER['REQUEST_URI']);
$showword = trim($words[count($words) - 1], '/');
echo $showword;

它对我不起作用.你能帮我吗?

It does not work for me. Can you help me please?

非常感谢!!

推荐答案

通过使用正则表达式:

preg_match("/[^/]+$/", "http://www.mydomainname.com/m/groups/view/test", $matches);
$last_word = $matches[0]; // test

相关文章