1

I'm trying to change a variable name in a query string, so it's usable by my PHP code.

The query gets posts from an external system, so I can't control that they are posting a variable name with a space in it. And that makes it impossible for me to use the PHP $_GET function.

I need to change variable%20name to ?new1

And I need to change variable2 to new2

There are many variables passed in the query, but only these two need to be changed. The rest can stay the same or even disappear.

So ?variable%20name=abc&variable2=xyz

Needs to end up as ?new1=abc&new2=xyz

Also, they may not be in this order and there may be more variables

So ?variable%20name=abc&blah=123&blah2=456&variable2=xyz

Could end up as ?new1=abc&new2=xyz

OR as ?new1=abc&blah=123&blah2=456&new2=xyz

Either way would be fine!

Please give me the mod_rewrite rule that will fix this.

Thank you in advance!

2
  • Could just parse $_SERVER['QUERY_STRING']. Commented Mar 27, 2011 at 18:14
  • Please tell me more and/or give me an example of the rewrite rule. Or is that a PHP function? I'm lost and need help to complete this. Thanks! Commented Mar 27, 2011 at 18:18

1 Answer 1

1

Parsing the query string with mod_rewrite is a bit of a pain, has to be done with RewriteCond and using %n replacements in a subsequent RewriteRule, probably easier to manually break up the original query string in PHP.

The full query string can be found (within PHP) in $_SERVER['QUERY_STRING'].

You can split it up using preg_split() or explode(), first on &, then on =, to get key/value pairs.


Using custom%20cbid=123&blahblahblah&name=example as an example.

$params = array();
foreach (explode("&", $_SERVER['QUERY_STRING']) as $cKeyValue) {
    list ($cKey, $cValue) = explode('=', $cKeyValue, 2);
    $params[urldecode($cKey)] = urldecode($cValue);
}

// Would result in:

$params = array('custom cbid' => 123,
                'blahblahblah' => NULL,
                'name' => example);
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you. Could you possibly give me the exact PHP code to take ?custom%20cbid=123&blahblahblah&name=example and end up with $cbid = 123; $mname = "example" ;
@Dustin: Example code added, without moving the parameters to separate variables, you can take them out of the resulting array if you wish.
Thank you! It seems I'm getting closer, but I still don't know how to deal with a variable that has a space in it. How can I make $cbid = 123? (i.e. how do i set the value of $cbid to the value for 'custom cbid'? That's what I really need to know how to do.
PS I really don't know what I'm doing, so when you say I can take the values out of the resulting array, I go and search Google for "values array php" and try to figure it out. But I haven't had luck yet...
Sweet, I got it to work with: $cbid = $params['custom%20cbid'];
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.