如果我有一个网址,比如www.example.com/test/example/product.html
我怎么能只得到测试部分(所以顶级(
我知道你会使用$_SERVER['REQUEST_URI']
,也许substr
或trim
但是我不确定该怎么做,谢谢!
使用 explode
将字符串拆分为数组,然后获取所需的部分。
$whatINeed = explode('/', $_SERVER['REQUEST_URI']);
$whatINeed = $whatINeed[1];
如果你使用 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
*/