note 33366 added to function.explode

From: Date: Tue, 24 Jun 2003 06:01:33 +0000
Subject: note 33366 added to function.explode
Groups: php.notes 
Request: Send a blank email to php-notes+get-50911@lists.php.net to get a copy of this message
This alters a parameter in a query string, if it doesn't exist adds it. May be useful to someone out there, it is a good example of the explode and implode functions. This code has been tested and proven working, there may allready be a function here that does this, I didn't have time to check all the posts. //------------------- //------------------- function AlterQueryStr($querystr, $param, $value) { $vars = explode("&", $querystr); $set = false; for ($i=0;$i<count($vars);$i++) { $v = explode('=',$vars[$i]); if ($v[0] == $param) { $v[1] = $value; $vars[$i] = implode('=', $v); $set = true; break; } } if (!$set) {$vars[] = $param . '=' . $value;} return ltrim(implode('&', $vars), '&'); } //------------------- //------------------- Examples and Results: AlterQueryStr('page=index&print=1&color=red', 'print', '0'); 'page=index&color=red' AlterQueryStr('page=index&print=1', 'color', 'red'); 'page=index&print=1&color=red' AlterQueryStr('', 'foo', 'bar'); 'foo=bar' -- http://spacevs.com ---- Manual Page -- http://www.php.net/manual/en/function.explode.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33366 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33366&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33366&report=yes

« previous php.notes (#50911) next »