Get last word from URL after a slash in PHP

后端 未结 8 1984
囚心锁ツ
囚心锁ツ 2020-12-06 00:50

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

8条回答
  •  不思量自难忘°
    2020-12-06 01:03

    To do that you can use explode on your REQUEST_URI.I've made some simple function:

    function getLast()
    {
        $requestUri = $_SERVER['REQUEST_URI'];
    
       # Remove query string
        $requestUri = trim(strstr($requestUri, '?', true), '/');
       # Note that delimeter is '/'
        $arr = explode('/', $requestUri);
        $count = count($arr);
    
        return $arr[$count - 1];
    }
    
    echo getLast();
    

提交回复
热议问题