Home >Backend Development >PHP Tutorial >php handles replacement link parameters

php handles replacement link parameters

Empty
EmptyOriginal
2017-08-24 09:00:522262browse

<?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;
}


The above is the detailed content of php handles replacement link parameters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn