note 21884 added to function.str-replace

From: Date: Thu, 30 May 2002 14:18:43 +0000
Subject: note 21884 added to function.str-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-31200@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 -- http://www.php.net/manual/en/function.str-replace.php http://master.php.net/manage/user-notes.php?action=edit+21884 http://master.php.net/manage/user-notes.php?action=delete+21884 http://master.php.net/manage/user-notes.php?action=reject+21884

« previous php.notes (#31200) next »