Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get second segment from url

Tags:

php

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 value where "first" stands ?

like image 719
georgevich Avatar asked Mar 28 '11 06:03

georgevich


2 Answers

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' 
like image 195
Gumbo Avatar answered Sep 23 '22 00:09

Gumbo


$segments = explode('/', trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/')); 
like image 24
cloetensbrecht Avatar answered Sep 24 '22 00:09

cloetensbrecht