request uri php: get first level

<?php
$url = 'http://username:[email protected]/test/example/product.html?arg=value#anchor';

print_r(parse_url($url));

$urlArray = parse_url($url);

/* Output:

Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /test/example/product.html
    [query] => arg=value
    [fragment] => anchor
)


*/

echo dirname($urlArray[path]);

/* Output:

/test    

*/

Split the string into an array with explode, and then take the part you need.

$whatINeed = explode('/', $_SERVER['REQUEST_URI']);
$whatINeed = $whatINeed[1];

If you use PHP 5.4, you can do $whatINeed = explode('/', $_SERVER['REQUEST_URI'])[1];

Tags:

Php