Home > Article > Backend Development > How to parse url in PHP and get url parameters (detailed tutorial)
The content of this article is about how to parse url and get url parameters in PHP (detailed tutorial). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Here are two ways to operate the url:
1. After getting a complete URL, how to parse the URL to get the parameters inside.
/** * 解析url中参数信息,返回参数数组 */ function convertUrlQuery($query) { $queryParts = explode('&', $query); $params = array(); foreach ($queryParts as $param) { $item = explode('=', $param); $params[$item[0]] = $item[1]; } return $params; }
2. How to splice an array into a url and pass it.
/** * 把数组拼接成url参数形式 */ function getUrlQuery($array_query) { $tmp = array(); foreach ($array_query as $k => $param) { $tmp[] = $k . '=' . $param; } $params = implode('&', $tmp); return $params; }
Test call:
$url = 'http://www.test.com/link?param1=1¶m2=2¶m3=3'; // 解析url,得到参数字符串 $url = parse_url($url); // 字符串->数组 $param_arr = $this->convertUrlQuery($url['query']); // 数组->字符串 $param_str = $this->getUrlQuery($param_arr);
Here are two ways to operate the url:
1. Get After reaching a complete URL, how to parse the URL to get the parameters inside.
/** * 解析url中参数信息,返回参数数组 */ function convertUrlQuery($query) { $queryParts = explode('&', $query); $params = array(); foreach ($queryParts as $param) { $item = explode('=', $param); $params[$item[0]] = $item[1]; } return $params; }
2. How to splice an array into a url and pass it.
/** * 把数组拼接成url参数形式 */ function getUrlQuery($array_query) { $tmp = array(); foreach ($array_query as $k => $param) { $tmp[] = $k . '=' . $param; } $params = implode('&', $tmp); return $params; }
Test call:
$url = 'http://www.test.com/link?param1=1¶m2=2¶m3=3'; // 解析url,得到参数字符串 $url = parse_url($url); // 字符串->数组 $param_arr = $this->convertUrlQuery($url['query']); // 数组->字符串 $param_str = $this->getUrlQuery($param_arr);
The above is the detailed content of How to parse url in PHP and get url parameters (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!