如果我有一个网址,如 www.example.com/test/example/product.html
我怎样才能获得测试部分(所以顶级)
我知道你会使用$_SERVER['REQUEST_URI']也许substr或者trim
但我不确定如何做到这一点,谢谢!
Sam*_*ane 21
将字符串拆分为数组explode,然后获取所需的部分.
$whatINeed = explode('/', $_SERVER['REQUEST_URI']);
$whatINeed = $whatINeed[1];
Run Code Online (Sandbox Code Playgroud)
如果你使用PHP 5.4,你可以做到 $whatINeed = explode('/', $_SERVER['REQUEST_URI'])[1];
<?php
$url = 'http://username:password@hostname.foo.bar/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
*/
Run Code Online (Sandbox Code Playgroud)