Forum Moderators: coopster

Message Too Old, No Replies

remove part of querystring held in variable

         

jackvull

11:46 am on Feb 9, 2006 (gmt 0)

10+ Year Member



Hi
I'm saving a querystring to a variable.
At times I need to strip out some of the querystring but not sure how to match this using pre_replace?

For example:
Querystring is?a=12345&b=23456
Querystring is?b=12345&a=23456&c=54321

How could I strip out the value a in both of these scenarios? I assume in the second one I wuold also need to remove the & before value c?

Thanks.

vevs

12:23 pm on Feb 9, 2006 (gmt 0)

10+ Year Member



this will do it for both values
preg_match("/((&a=)¦(\?a=))([a-zA-Z0-9]+)(&)/",$value,$match);
echo $match[4];

regards

jackvull

12:48 pm on Feb 9, 2006 (gmt 0)

10+ Year Member



Thanks.
Still slightly lost. Currently, I've got:

$newstr = preg_replace
(
"/((&a=)¦(\?a=))([a-zA-Z0-9]+)(&)/",
"",
$string
);

There's still one more scenario to replace and thats when it's on the end of the querystring e.g.
?b=1234&c=1234&a=1234567

The above expression always looks for an & on the end of the string so would I have to have a pattern array also looking for:
"/((&a=)¦(\?a=))([a-zA-Z0-9])/"

jackvull

1:25 pm on Feb 9, 2006 (gmt 0)

10+ Year Member



My worry is that this:
"/((&a=)¦(\?a=))([a-zA-Z0-9])/"

would replace the entire querystring because it matches any alphanumeric characters

coopster

2:45 pm on Feb 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Also have a look at parse_url [php.net] and parse_str [php.net].

vevs

3:05 pm on Feb 9, 2006 (gmt 0)

10+ Year Member



this work for the last scenario
preg_match("/((&a=)¦(\?a=))([a-zA-Z0-9]+)(&?)/",$value,$match);

and this will match only if there are numbers after a=
preg_match("/((&a=)¦(\?a=))([0-9]+)(&?)/",$value,$match);