note 48407 deleted from function.parse-url by didou

From: Date: Sat, 03 Feb 2007 03:08:29 +0000
Subject: note 48407 deleted from function.parse-url by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-122617@lists.php.net to get a copy of this message
Note Submitter: corey at eyewantmedia dot com ---- Just in case this helps someone, this is a quick and dirty way to add or replace values to your query string, which I found especially useful when modifying urls to allow paging through resultsets. /** * @return string * @param string $varName * @param string $varVal * @param string $uri * @desc Returns the a string that is either * $uri if you pass it or the current * uri with the variable name $varName * equal to the value urlencode($varVal) * It replaces a current value if it find * it or adds the variable and value pair * if they are new. */ function AddToQuery($varName, $varVal, $uri=null) { $result = ''; $beginning = ''; $ending = ''; if (is_null($uri)) {//Piece together uri string $beginning = $_SERVER['PHP_SELF']; $ending = ( isset($_SERVER['QUERY_STRING']) ) ? $_SERVER['QUERY_STRING'] : ''; } else { $qstart = strpos($uri, '?'); if ($qstart === false) { $beginning = $uri; //$ending is '' anyway } else { $beginning = substr($uri, 0, $qstart); $ending = substr($uri, $qstart); } } if (strlen($ending) > 0) { $vals = array(); $ending = str_replace('?','', $ending); parse_str($ending, $vals); $vals[$varName] = $varVal; $ending = ''; $count = 0; foreach($vals as $k => $v) { if ($count > 0) { $ending .= '&'; } else { $count++; } $ending .= "$k=" . urlencode($v); } } else { $ending = $varName . '=' . urlencode($varVal); } $result = $beginning . '?' . $ending; return $result; }

« previous php.notes (#122617) next »