georgevich georgevich - 1 year ago 130
PHP Question

Get second segment from url

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


How to get the value where "first" stands ?

Answer Source

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 you will get 'user'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download