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

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

问题描述

我需要从网址中获取最后一个字。因此,例如,我有以下URL:



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



我只需要用PHP获得test,没有其他的。我试图使用这样的东西:

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

它对我不起作用。你可以帮我吗?



非常感谢你!

使用正则表达式:

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


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

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?

Thank you so much!!

解决方案

by using regex:

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

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

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