以下代码用来实现带参数跳转
<?php function removeFromQueryStringByKey($queryString,$key){ $queryString = preg_replace("/$key.*&/","", $queryString); $queryString = preg_replace("/&$key=.*/", "", $queryString); return $queryString; } function getQueryStringArr($s){ $result = array(); if ($s != ""){ $array = explode("&", $s); foreach ($array as $item){ $I = explode("=", $item); $result[$I[0]] = $I[1]; } } return $result; } function mergeQueryString($currentQueryString,$newQueryString){ $currentArr = getQueryStringArr($currentQueryString); $newArr = getQueryStringArr($newQueryString); $result = array_replace($currentArr, $newArr); return $result; } function redirect($location,$currentQueryString,$newQueryString){ if($location == ""){ $location = "/index.php"; } $queryString = ""; $queryStringArr = mergeQueryString($currentQueryString, $newQueryString); if (!empty($queryStringArr)){ foreach ($queryStringArr as $key => $value){ $queryString .= "&". $key.'='.$value; } $queryString = substr_replace($queryString, "?", 0, 1); } header("location:".$location.$queryString); } ?>
实例:
1、删除querystring的关键字
removeFromQueryStringByKey("a=2&b=3","a");
结果:b=3
2、带参数跳转
redirect("/index.php","a=2&b=3","b=4");
结果:跳转到 /index.php?a=2&b=4
redirect("/index.php","","")
结果:跳转到 /index.php
redirect("/index.php","a=2&b=3","");
结果:跳转到 /index.php?a=2&b=3
redirect("/index.php","","b=4");
结果:跳转到 /index.php?b=4