note 28063 deleted from function.parse-url by didou
| From: | didou@php.net | Date: | Thu, 09 Oct 2003 13:28:34 +0000 |
| Subject: | note 28063 deleted from function.parse-url by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-58128@lists.php.net to get a copy of this message | ||
Note Submitter: bermi.ferrer ) a t ( akelos d·o·t com
----
This function allows your script to delete a GET VAR from your URL.
This could be done in many different ways,
but this is first one I found (not the best :P )
Imagine we have the following values:
$url = "http://www.php.net/test.php?value1=test1&value2=test2";
$value_to_delete = "value1";
$new_url = delete_value_from_url($url, $value_to_delete);
after delete_value_from_url() $new_url will be
http://www.php.net/test.php?value2=test2
//HERE IS THE FUNCTION
function delete_value_from_url($url, $value_to_delete)
{
//First we separate the URL into the possible delimiters
$parts = preg_split("(&|\?)",$url);
//Only if separated parts have more than one array element
if(count($parts) > 0)
{
//We move thru the array searching for matches
foreach($parts as $part)
{
//Now we need to guess which separator use to join the parts
$i++;
if($i == 1) $separator = "?"; else $separator = "&";
//If it's a match, we fill $result with corresponding values
if(!eregi("($value_to_delete)(=)",$part))
$result .= $part.$separator;
}//END foreach($parts as $part)
//Now we delete last & from the $result string
$result = substr($result, 0, (strlen($result)-1));
}//END if(count($parts) > 0)
//The URL doesn't have any GET value, so ve return the initial URL
else
{
$result = $url;
}
return $result;
}//END delete_value_from_url
I hope this is usefull for someone