所以我想说我有这样一个链接:
http://mywebsite.com/profile/alexkvazos/
我想爆解网址并获取此信息:
$uri = array('profile','alexkvazos');
最简单的方法是什么?
答案 0 :(得分:2)
试
// if the url is http://www.example.com/foo/bar/wow
function getUriSegments() {
return explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
}
print_r(getUriSegments()); //returns array(0=>'foo', 1=>'bar', 2=>'wow')
来源: - http://www.timwickstrom.com/server-side-code/php/php-get-uri-segments/
答案 1 :(得分:0)
$path_parts = parse_url('http://mywebsite.com/profile/alexkvazos/');
$uri = explode('/',trim($path_parts['path'],'/'));
print_r($uri);
答案 2 :(得分:0)
你可以使用这段代码:
<?php
$url = 'http://mywebsite.com/profile/alexkvazos/';
$url = rtrim($url,'/'); // lose the trailing slash
$urlArr = explode("/", $url );
$urlArr = array_reverse( $urlArr );
echo $urlArr[0];
?>
答案 3 :(得分:-1)
我不会使用parse_url
,我会像这样使用preg_split
:
$segments = preg_split('#/#', $_SERVER['REQUEST_URI']);
$final = array();
foreach($segments as $key => $value) {
if($value != '') {
$final[] = $value;
}
}
var_dump($final);