Get last word from URL after a slash in PHP

2019-01-18 10:45发布

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!!

标签: php url get
8条回答
冷血范
2楼-- · 2019-01-18 11:35
ex: $url      = 'http://www.youtube.com/embed/ADU0QnQ4eDs';
$url      = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url_path = parse_url($url, PHP_URL_PATH);
$basename = pathinfo($url_path, PATHINFO_BASENAME);
// **output**: $basename is "ADU0QnQ4eDs"

complete solution you will get in the below link. i just found to Get last word from URL after a slash in PHP.

Get last parameter of url in php

查看更多
Bombasti
3楼-- · 2019-01-18 11:36

by using regex:

preg_match("/[^\/]+$/", "http://www.mydomainname.com/m/groups/view/test", $matches);
$last_word = $matches[0]; // test
查看更多
登录 后发表回答