首页  >  文章  >  后端开发  >  php处理替换链接参数

php处理替换链接参数

Empty
Empty原创
2017-08-24 09:00:522244浏览

<?php
$url = &#39;http://www.baidu.com/index.php?m=content&c=index&a=lists&catid=6&area=0&author=0&h=0&region=0&s=1&page=1&#39;;
$arr = parse_url($url);
print_r($arr);
$arr_query = convertUrlQuery($arr[&#39;query&#39;]);
print_r(getUrlQuery($arr_query));
/**
* 将字符串参数变为数组
* @param $query
* @return array array (size=10)
                    &#39;m&#39; => string &#39;content&#39; (length=7)
                    &#39;c&#39; => string &#39;index&#39; (length=5)
                    &#39;a&#39; => string &#39;lists&#39; (length=5)
                    &#39;catid&#39; => string &#39;6&#39; (length=1)
                    &#39;area&#39; => string &#39;0&#39; (length=1)
                    &#39;author&#39; => string &#39;0&#39; (length=1)
                    &#39;h&#39; => string &#39;0&#39; (length=1)
                    &#39;region&#39; => string &#39;0&#39; (length=1)
                    &#39;s&#39; => string &#39;1&#39; (length=1)
                    &#39;page&#39; => string &#39;1&#39; (length=1)
*/
function convertUrlQuery($query)
{
    $queryParts = explode(&#39;&&#39;, $query);
    $params = array();
    foreach ($queryParts as $param) {
        $item = explode(&#39;=&#39;, $param);
        $params[$item[0]] = $item[1];
    }
    return $params;
}
/**
* 将参数变为字符串
* @param $array_query
* @return string string &#39;m=content&c=index&a=lists&catid=6&area=0&author=0&h=0&region=0&s=1&page=1&#39; (length=73)
*/
function getUrlQuery($array_query)
{
    $tmp = array();
    foreach($array_query as $k=>$param)
    {
        if($k==&#39;ali_trackid&#39;){
            $tmp[] = $k.&#39;=&#39;.&#39;2:mm_58345187_23388879_77836075:1495808287_3k6_1652169695&#39;;
        }else{
            $tmp[] = $k.&#39;=&#39;.$param;
        }
    }
    $params = implode(&#39;&&#39;,$tmp);
    return $params;
}


以上是 php处理替换链接参数的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn