note 21884 deleted from function.str-replace by victor
| From: | victor@php.net | Date: | Sat, 09 Nov 2002 18:09:36 +0000 |
| Subject: | note 21884 deleted from function.str-replace by victor | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-39227@lists.php.net to get a copy of this message | ||
If you need to replace a parameter in a url, for example change
http://www.test.com/index.php?dummy=4&test=12
in
http://www.test.com/index.php?dummy=5&test=12,
use the following function:
This is what it does:
If the parameter exists, it changes its value.
If it doesn't, it adds it at the end of the URL.
function ChangeParameter($param,$value,$url)
{
$parsed=parse_url($url);
if ($parsed["query"]=="")
return ($url."?".$param."=".$value);
if(substr_count($parsed["query"],$param."=")>0)
{
$array=explode($param."=",$parsed["query"]);
$array2=explode("&",$array[1]);
//Beware i had to cut next line
$parsed["query"]=str_replace($param."=".$array2[0],$pa%
ram."=".$value,$parsed["query"]);
return glue_url($parsed);
}
else
return ($url."&".$param."=".$value);
}
-Feanor