note 69197 deleted from function.ereg by bjori
| From: | bjori@php.net | Date: | Sat, 26 Aug 2006 17:05:16 +0000 |
| Subject: | note 69197 deleted from function.ereg by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-116367@lists.php.net to get a copy of this message | ||
Note Submitter: liquix at hjelpesentral dot no
Reason: bad code
----
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) ? "?" : "&";
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 .= "&";
}
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".