*
* @param string $url url that must be modified
* @param string $var GET variable name
- * @param string $val variable value
+ * @param string $val variable value (CANNOT be an array)
* @param boolean $link controls sanitizing of ampersand in urls (since 1.3.2)
+ * @param boolean $treat_as_array When TRUE, if $var is an array (it occurs one
+ * or more times with square brackets after it,
+ * e.g. "var[1]"), the whole array will be removed
+ * (when $val is NULL) or the given value will be
+ * added to the next array slot (@since 1.4.23/1.5.2)
*
* @return string $url modified url
*
* @since 1.3.0
*
*/
-function set_url_var($url, $var, $val=null, $link=true) {
+function set_url_var($url, $var, $val=null, $link=true, $treat_as_array=false) {
$url = str_replace('&','&',$url);
if (strpos($url, '?') === false) {
$newpar = array();
$params = explode('&', $params);
+ $array_names = array();
foreach ($params as $p) {
if (trim($p)) {
$p = explode('=', $p);
$newpar[$p[0]] = (isset($p[1]) ? $p[1] : '');
+ if ($treat_as_array && preg_match('/(.*)\[(\d+)]$/', $p[0], $matches)) {
+ if (!is_array($array_names[$matches[1]])) $array_names[$matches[1]] = array();
+ $array_names[$matches[1]][$matches[2]] = $p[1];
+ }
}
}
if (is_null($val)) {
- unset($newpar[$var]);
+ if ($treat_as_array && !empty($array_names[$var])) {
+ foreach ($array_names[$var] as $key => $ignore)
+ unset($newpar[$var . '[' . $key . ']']);
+ } else {
+ unset($newpar[$var]);
+ }
} else {
- $newpar[$var] = $val;
+ if ($treat_as_array && !empty($array_names[$var])) {
+ $max_key = 0;
+ foreach ($array_names[$var] as $key => $ignore)
+ if ($key >= $max_key) $max_key = $key + 1;
+ $newpar[$var . '[' . $max_key . ']'] = $val;
+ } else {
+ $newpar[$var] = $val;
+ }
}
if (!count($newpar)) {