在 PHP 中的斜杠后从 URL 中获取最后一个单词 [英] Get last word from URL after a slash in PHP

查看:37
本文介绍了在 PHP 中的斜杠后从 URL 中获取最后一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 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

这篇关于在 PHP 中的斜杠后从 URL 中获取最后一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆