regular experssion help
| From: | Robert V. Zwink | Date: | Fri, 02 Nov 2001 14:15:18 +0000 |
| Subject: | regular experssion help | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-73264@lists.php.net to get a copy of this message | ||
I hope someone has time to help with a regular expression?
I need a regex that will replace get variables, regardless of there location
in a URL, and if the get var does not exist I hope that it will create the
get var in the URL. Some examples
$REQUEST_URI can look like this "value1":
filename.phtml?test=value1
filename.phtml?another=value&test=value1
filename.phtml
filename.phtml?another=value
The variable $test will either exist preceded by "?" "&" or not at all.
I would like to take a value pair, put it into a function with $REQUEST_URI
and the have a new $REQUEST_URI returned with either the value replaced or
added.
Replacing/adding "value2"
filename.phtml?test=value2
filename.phtml?another=value&test=value2
filename.phtml?test=value2
filename.phtml?another=value&test=value2
The variable located by matching ?test &test or adding it if it does not
exist, with a "?" or a "&"
I currently have a function:
replace_get_var_value($string, $var, $new_value){
return eregi_replace("&$var=([a-z0-9\+]+)", "&$var=$new_value",
$string);
}
But it won't work if the value does not exist, or if the value is the first
variable in the set within $REQUEST_URI.
Example ($REQUEST_URI that will not work with this function)
filename.phml?another=value
filename.phml?var=old_value
filename.phml
The only $REQUEST_URI that this will work with is
filename.phtml?another=value&var=old_value
I hope my description makes sense. Thanks for any help!
-Rob Z.
http://www.zwink.net