PHP 获取当前URL参数 字符串转数组 数组转字符串
<?php $url = 'http://www.xiaoguan.net/posts?page=3'; $arr = parse_url($url); var_dump($arr); $arr_query = convertUrlQuery($arr['query']); var_dump($arr_query); var_dump(getUrlQuery($arr_query)); /** * 将字符串参数变为数组 * @param $query * @return array array */ function convertUrlQuery($query) { $queryParts = explode('&', $query); $params = array(); foreach ($queryParts as $param) { $item = explode('=', $param); $params[$item[0]] = $item[1]; } return $params; } /** * 将参数变为字符串 * @param $array_query * @return string string */ function getUrlQuery($array_query) { $tmp = array(); foreach($array_query as $k=>$param) { $tmp[] = $k.'='.$param; } $params = implode('&',$tmp); return $params; }