Php - 在PHP脚本之后获取路径

时间:2013-11-21 05:22:46

标签: php html apache path

我有搜索高和低的答案我的问题,我找不到它。基本上我想要做的是在php脚本之后获取路径。恩。 “http://www.example.com/index.php/arg1/arg2/arg3/etc/”并在数组中获取arg1,arg2,arg3等。我怎么能在PHP中这样做,一旦我这样做,“http://www.example.com/arg1/arg2/arg3/etc”仍然会返回相同的结果。如果没有,那我该怎么做呢?

3 个答案:

答案 0 :(得分:3)

以下是如何获得答案,以及将来您将拥有的其他一些答案。制作一个脚本,例如“test.php”,只需将这一行放入其中:

<?php phpinfo();

然后使用http://www.example.com/test.php/one/two/three/etc

浏览到它

查看$_SERVER值,找出与之相符的值。

在$ _SERVER [“PATH_INFO”]中我看到了你想要的答案:“/ one / two / three / etc” 然后使用explode将其转换为数组:

print_r( explode('/',$_SERVER['PATH_INFO'] );

但有时候$_SERVER["REQUEST_URI"]会成为你想要的那个,特别是在使用URL重写时。

<强>更新

回应评论,这是我要做的事情:

$args = explode('/',$_SERVER['REQUEST_URI']);
if($args[0] == 'index.php')array_shift($args);

答案 1 :(得分:1)

试试这样:

$url ="http://www.example.com/index.php/arg1/arg2/arg3/etc/";


$array =  explode("/",parse_url($url, PHP_URL_PATH));
if (in_array('index.php', $array)) 
{
    unset($array[array_search('index.php',$array)]);
}


print_r(array_values(array_filter($array)));

答案 2 :(得分:0)

您可以尝试使用parse_urlparse_str

吗?
$url =$_SERVER['REQUEST_URI'];

 $ParseUrl = parse_url($url);
 print_r($ParseUrl);

 $arr =  parse_str($ParseUrl['query']);

 print_r($arr);