note 69197 added to function.ereg

From: Date: Sat, 26 Aug 2006 16:51:16 +0000
Subject: note 69197 added to function.ereg
Groups: php.notes 
Request: Send a blank email to php-notes+get-116364@lists.php.net to get a copy of this message
I created a simple function for adding _GET vars to an URL. It will overwrite vars if they exists instead of just adding the variables again!(Example: Instead of index.php?test=test&test=tut this will be shown: index.php?test=tut, since tut was the last _GET var) <?php function url_addgetvar($vars, $values, $url) { if(!is_array($vars) || !is_array($values)) { return false; } if(count($vars) != count($values)) { return false; } for($i = 0; $i < count($vars); $i++) { if(ereg($vars[$i] . "=.+", $url)) { $parameters = preg_split("[\?\&]", $url); $url = $parameters[0]; for($j = 1; $j < count($parameters); $j++) { $tmp_pars = explode("=", $parameters[$j]); $separator = ($j == 1) ? "?" : "&amp;"; if($tmp_pars[0] == $vars[$i] && !empty($vars[$i])) { $tmp_pars[1] = $vars[$i]; } $url .= $separator . $tmp_pars[0] . "=" . $tmp_pars[1]; } } else { if(ereg("^.+\?.+=.+$", $url)) { $url .= "&amp;"; } else { $url .= "?"; } $url .= $vars[$i] . "=" . $values[$i]; } } return $url; } ?> It will be used like this: <?php if(!$url = url_addgetvar(array('one', 'two'), array('number', 'not'), $_SERVER['REQUEST_URI'])) { die("Couldn't add GET parameters"); } ?> If the URL originaly is "/hb/test.php?one=tret" it will return "/hb/test.php?one=number&two=not". ---- Server IP: 83.137.200.2 Probable Submitter: 80.202.217.211 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.ereg.php Edit -- https://master.php.net/note/edit/69197 Del: integrated -- https://master.php.net/note/delete/69197/integrated Del: useless -- https://master.php.net/note/delete/69197/useless Del: bad code -- https://master.php.net/note/delete/69197/bad+code Del: spam -- https://master.php.net/note/delete/69197/spam Del: non-english -- https://master.php.net/note/delete/69197/non-english Del: in docs -- https://master.php.net/note/delete/69197/in+docs Del: other reasons-- https://master.php.net/note/delete/69197 Reject -- https://master.php.net/note/reject/69197 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#116364) next »