Well, I think I found the solution. I changed the regex a bit, and added a
condition. This should work, but if anyone has a better idea please let me
know.
function replace_get_var_value($string, $var, $new_value){
// substitute value
$buffer = eregi_replace("([?|&]{1})$var=([a-z0-9\+]+)",
"\\1$var=$new_value", $string);
// value not found then add it to the REQUEST_URI
if($buffer==$string){
if(strchr("?", $buffer)){
$buffer = $buffer."&$var=$new_value";
}else{
$buffer = $buffer."?$var=$new_value";
}
}
return $buffer;
}
echo replace_get_var_value($REQUEST_URI, "test", "value2");
Thanks!
Robert Zwink
http://www.zwink.net
-----Original Message-----
From: Robert V. Zwink [mailto:[EMAIL PROTECTED]]
Sent: Friday, November 02, 2001 9:15 AM
To: [EMAIL PROTECTED]
Subject: [PHP] regular experssion help
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]