Get characters after last / in url

后端 未结 8 881
梦谈多话
梦谈多话 2020-11-30 20:05

I want to get the characters after the last / in an url like http://www.vimeo.com/1234567

How do I do with php?

相关标签:
8条回答
  • 2020-11-30 20:34

    You can use substr and strrchr:

    $url = 'http://www.vimeo.com/1234567';
    $str = substr(strrchr($url, '/'), 1);
    echo $str;      // Output: 1234567
    
    0 讨论(0)
  • 2020-11-30 20:39

    Here's a beautiful dynamic function I wrote to remove last part of url or path.

    /**
     * remove the last directories
     *
     * @param $path the path
     * @param $level number of directories to remove
     *
     * @return string
     */
    private function removeLastDir($path, $level)
    {
        if(is_int($level) && $level > 0){
            $path = preg_replace('#\/[^/]*$#', '', $path);
            return $this->removeLastDir($path, (int) $level - 1);
        }
        return $path;
    }
    
    0 讨论(0)
提交回复
热议问题