Get second segment from url

前端 未结 4 963
悲&欢浪女
悲&欢浪女 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:37

    To take your example http://domain.com/first/second

    $segments = explode('/', trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'));
    $numSegments = count($segments); 
    $currentSegment = $segments[$numSegments - 1];
    
    echo 'Current Segment: ' , $currentSegment;
    

    Would result in Current Segment: second

    You can change the numSegments -2 to get first

提交回复
热议问题