如何从PHP的URL字符串中提取查询参数? [英] How do I extract query parameters from a URL string in PHP?

查看:549
本文介绍了如何从PHP的URL字符串中提取查询参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户可以在我的网站上使用HTML表单输入URL,因此他们可以输入以下内容:

Users can input URLs using a HTML form on my website, so they might enter something like this: http://www.example.com?test=123&random=abc, it can be anything. I need to extract the value of a certain query parameter, in this case 'test' (the value 123). Is there a way to do this?

推荐答案

您可以使用 parse_url parse_str 像这样:

You can use parse_url and parse_str like this:

$query = parse_url('http://www.example.com?test=123&random=abc', PHP_URL_QUERY);
parse_str($query, $params);
$test = $params['test'];

parse_url 允许将URL分为不同部分(方案,主机,路径,查询,等等);在这里,我们使用它仅获取查询(test=123&random=abc).然后,我们可以使用 parse_str 解析查询.

parse_url allows to split an URL in different parts (scheme, host, path, query, etc); here we use it to get only the query (test=123&random=abc). Then we can parse the query with parse_str.

这篇关于如何从PHP的URL字符串中提取查询参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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