<?php
//例舉一個URL格式的字元串:
$str = 'http://www.vvdisk.com/?param1=10&m2=20&m3=30&m4=40&m5=50&m6=60';
//1.0 用parse_url解析URL,此處是$str
$arr = parse_url($str);
var_dump($arr);
//2.0 将URL中的參數取出來放到數組裡
$arr_query = convertUrlQuery($arr['query']);
var_dump($arr_query);
//3.0 将 參數數組 再變回 字元串形式的參數格式
var_dump(getUrlQuery($arr_query));
/**
* Returns the url query as associative array
*
* @param string query
* @return array params
*/
function convertUrlQuery($query)
{
$queryParts = explode('&', $query);
$params = array();
foreach ($queryParts as $param)
{
$item = explode('=', $param);
$params[$item[0]] = $item[1];
}
return $params;
}
function getUrlQuery($array_query)
{
$tmp = array();
foreach($array_query as $k=>$param)
{
$tmp[] = $k.'='.$param;
}
$params = implode('&',$tmp);
return $params;
?>