在php案例中获取url查询参数

在php案例中获取url查询参数,php,Php,我有以下index.php文件,它将处理所有路由: <!DOCTYPE html> <html> <body> <?php $request=$_SERVER['REQUEST_URI']; switch($request){ case '/': require __DIR__.'/index1.php';break; case '/home':require __DIR__.'/views/home.php';brea

我有以下index.php文件,它将处理所有路由:

<!DOCTYPE html>
<html>
<body>

    <?php
    $request=$_SERVER['REQUEST_URI'];
switch($request){
    case '/': require __DIR__.'/index1.php';break;
    case '/home':require __DIR__.'/views/home.php';break;
    default: echo("wrong");}
?>
</body>
</html>

下面的文件是home.php:

<html>
<body>
<?php
$str=$_SERVER['QUERY_STRING'];
echo($str);
?>
</body>
</html>

如果没有查询字符串,上述设置可以正常工作。但是我如何包括用户点击
127.0.0.1/主页的条件?1234
。在这种情况下,任何case语句都将无效,并返回错误。我该如何更改它,使其与查询字符串一起转到适当的路由

提前谢谢

使用


不要使用正则表达式或字符串函数。用于从URL提取路径(或任何部分):

$path=parse\u url(“/home?1234”,PHP\u url\u path);
#$path将为“/主”
交换机($path){
...

我将如何获取查询字符串1234?传递
PHP\u URL\u query
$\u服务器[“query\u string”]
$uri = explode('?', $_SERVER['REQUEST_URI'], 2);
$str = $uri[0]