如何更改 foo 链接中的变量?q=some&s=3&d=new
考虑使用 foo?q=some&s=3&d=new
的 URL 访问的 PHP 脚本。我想知道是否有一个实用的方法来解析 url 以创建带有新变量的链接(在 php 页面内)。例如 foo?q=**another-word**&s=3&d=new
或 foo?q=another-word&s=**11**& d=new
我正在考虑通过 $_SERVER['REQUEST_URI'] 捕获请求的 URL,然后用正则表达式进行解析;但这在实践中并不是一个好主意。应该有一种方便的方法来解析附加到 php 脚本的变量。实际上是GET方法的逆操作。
Consider a php script visited with URL of foo?q=some&s=3&d=new
. I wonder if there is a paractical method for parsing the url to create links with new variable (within php page). For example foo?q=**another-word**&s=3&d=new
or foo?q=another-word&s=**11**&d=new
I am thinking of catching the requested URL by $_SERVER['REQUEST_URI'] then parsing with regex; but this is not a good idea in practice. There should be a handy way to parse variables attached to the php script. In fact, inverse action of GET method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
$_GET
变量包含当前查询字符串的已解析数组。数组联合运算符+
可以轻松地将新值合并到其中。http_build_query
将它们重新组合到一个查询字符串中:如果您需要对 URL 进行更多解析要获取
'foo'
,请使用parse_url
在REQUEST_URI
上。The
$_GET
variable contains an already parsed array of the current query string. The array union operator+
makes it easy to merge new values into that.http_build_query
puts them back together into a query string:If you need more parsing of the URL to get
'foo'
, useparse_url
on theREQUEST_URI
.使用
http_build_query
怎么样? http://php.net/manual/en/function.http-build -query.php它将允许您从数组构建查询字符串。
What about using
http_build_query
? http://php.net/manual/en/function.http-build-query.phpIt will allow you to build a query string from an array.
我会使用
parse_str
:结果是:
虽然,这个方法可能看起来像“困难的方法”:)
I'd use
parse_str
:Result is:
Although, this method might look like "the hard way" :)