Helpful Information
 
 
Category: Post a PHP snippet
Replace url variable / add variable function.

Hey everyone,
This is the function I use when replacing url variables, or adding a new variable.

I realize there are holes but for the most part as long as there are no crazy characters in the url or the variables you will be ok.



function replaceUrl($url,$uKey,$uVal){
$Qurl = explode("?",$url);
$arr = explode("&",$Qurl[1]);
$r = ''; $set = 0;
for($i=0;$i<sizeof($arr);$i++){
$arr2 = explode("=",$arr[$i]);
if($arr2[0] == $uKey){
$arr2[1] = $uVal;
$set = 1;
}
$r.=$arr2[0]."=".$arr2[1]."&";
}
if($set == 0){
$r.= $uKey."=".$uVal;
} else {
$r = rtrim($r, "&");
}
return $Qurl[0].'?'.$r;
}


usage:




print replaceUrl('http://yourDomain.com/index.php?test=1&name=rlemon&age=19','name','rob lemon');










privacy (GDPR)