Get second segment from url

前端 未结 4 953
悲&欢浪女
悲&欢浪女 2020-12-25 11:53

How to get the second segment in URL without slashes ? For example I have a URL`s like this

http://foobar/first/second

How to get the valu

4条回答
  •  攒了一身酷
    2020-12-25 12:55

    Use parse_url to get the path from the URL and then use explode to split it into its segments:

    $uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    $uri_segments = explode('/', $uri_path);
    
    echo $uri_segments[0]; // for www.example.com/user/account you will get 'user'
    

提交回复
热议问题