note 48407 added to function.parse-url

From: Date: Wed, 22 Dec 2004 17:18:02 +0000
Subject: note 48407 added to function.parse-url
Groups: php.notes 
Request: Send a blank email to php-notes+get-82180@lists.php.net to get a copy of this message
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; } ---- Manual Page -- http://www.php.net/manual/en/function.parse-url.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+48407 Delete -- http://master.php.net/manage/user-notes.php?action=delete+48407&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48407&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48407&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48407&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48407&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48407&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+48407&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#82180) next »