在 php 中解析 URL [英] Parse URL in php

查看:23
本文介绍了在 php 中解析 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 php 中,如果我想解析一个 URL,例如 www.site.com/index.php?foo=bar,我可以使用 _POST 变量.我可以通过 _POST['foo'] 检索 bar.

In php, if I wanted to parse a URL, such as www.site.com/index.php?foo=bar, I can do with the _POST variable. I can retrieve bar by _POST['foo'].

但是如果我的 URL 类似于 www.site.com/index.php?foo&bar 呢?即,我没有像第一种情况那样将参数作为键值对.如何在 php 中解析这个?

But what if my URL is something like www.site.com/index.php?foo&bar. i.e., I don't have the arguments as key-value pairs as in the first case. How to parse this in php?

推荐答案

请求像这样的 URL 是一种误解

It's a misconception that requesting a URL like

http://www.example.com/index.php?foo=bar

会在 $_POST['bar'] 中给你 bar.Url 参数将填充 $_GET.任何应该显示在 $_POST 中的内容都必须在请求正文中提交.请参阅如何在 HTTP POST 请求中发送参数? 了解一些细节.

would give you bar in $_POST['bar']. Url parameters will populate $_GET. Anything that's supposed to show up in $_POST has to be submitted in the Request body. See How are parameters sent in an HTTP POST request? for some details.

澄清后,空 URL 参数根本不是问题.像

With that clarified, empty URL parameters are not a problem at all. A URL like

http://www.example.com/index.php?foo&bar

将用空值填充 $_GET['foo']$_GET['bar'].

will populate $_GET['foo'] and $_GET['bar'] with empty values.

这篇关于在 php 中解析 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆