在 PHP 中解析 URL 查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20750367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:30:31  来源:igfitidea点击:

Parsing URL query in PHP

phpparsingurl

提问by Maelish

Assuming a url of www.domain.org?x=1&y=2&z=3, what would be a smart method to separate out the query elements of a url in php without using GET or REQUEST?

假设一个 url www.domain.org?x=1&y=2&z=3,在不使用 GET 或 REQUEST 的情况下,在 php 中分离出 url 的查询元素的智能方法是什么?

  $url = parse_url($url);
  echo $url[fragment];    

I don't think it's possible to return query parts separately is it? From what I can tell the query will just say x=1&y=2&z=3but please let me know if I am wrong. Otherwise, what would you do to parse the $url[query]?

我认为不可能单独返回查询部分是吗?据我所知,查询只会说,x=1&y=2&z=3但如果我错了,请告诉我。否则,你会怎么做来解析$url[query]

Edit: Fragment should be Query instead. Sorry for the confusion, I am learning!

编辑:片段应该是查询。抱歉打扰了,学习了!

回答by Trenton Trama

You can take the second step and parse the query string using parse_str.

您可以执行第二步并使用parse_str解析查询字符串。

$url = 'www.domain.org?x=1&y=2&z=3';
$url_parts = parse_url($url);
parse_str($url_parts['query'], $query_parts);
var_dump($query_parts);

I assumed you meant the query string instead of the fragment because there isn't a standard pattern for fragments.

我假设您指的是查询字符串而不是片段,因为片段没有标准模式。

回答by zavg

parse_urlfunction returns several components including query. To parse it you should run parse_str.

parse_url函数返回几个组件,包括query. 要解析它,您应该运行parse_str.

$parsedUrl = parse_url($url);
parse_str($parsedUrl['query'], $parsedQueryString);

If you are going just to parse your HTTP request URL:

如果您只想解析您的 HTTP 请求 URL:

  • use $_REQUEST['x'], $_REQUEST['y'], $_REQUEST['z']variables to access x,y,z parameters;

  • use $_SERVER['QUERY_STRING']to get whole URL querystring.

  • 使用$_REQUEST['x'], $_REQUEST['y'],$_REQUEST['z']变量访问 x,y,z 参数;

  • 用于$_SERVER['QUERY_STRING']获取整个 URL 查询字符串。

回答by Maelish

I was getting errors with some of the answers above but they did lead me to the right answer. Thanks guys.

我在上面的一些答案中遇到了错误,但它们确实让我找到了正确的答案。谢谢你们。

 $url = 'www.domain.org?x=1&y=2&z=3';
 $query = $url[query]; 
 parse_str($query);
 echo "$x &y $z";

And this outputs: 1 2 3, which is what I was trying to figure out.

这输出:1 2 3,这就是我想要弄清楚的。

回答by Timo Huovinen

As a one liner with no error checking

作为没有错误检查的单班轮

parse_str(parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY), $query);

$querywill contain the query string parameters
unlike PHP's $_GET, this will work with query params of any length

$query将包含
与 PHP 不同的查询字符串参数$_GET,这将适用于任何长度的查询参数

回答by M K

I highly recommend using this url wrapper https://github.com/weew/url(which I have written)

我强烈建议使用这个 url 包装器https://github.com/weew/url(我写的)

It is capable of parsing urls of complexity like protocol://username:[email protected]:80/some/path?query=value#fragmentand has many more goodies.

它能够解析复杂的 url,比如protocol://username:[email protected]:80/some/path?query=value#fragment并且有更多的好东西