1

I am sending a urlencoded string to a system which they are then sending back to me, but the URL encoded characters have a lowercase instead of uppercase character in them.

Is there an easy way for me to find each instance and change it to uppercase?

Here's an example

What I am sending:

6Vc6K83k%2FnBWb6sOfc0fcQiMOttpKJci9o%2B%2BdMBs0MCGJgQkgyr6zV%2FO8hqRATKW1uUYEs5zOqKso36x%2BydCc6

What I get in return:

6Vc6K83k%2fnBWb6sOfc0fcQiMOttpKJci9o%2b%2bdMBs0MCGJgQkgyr6zV%2fO8hqRATKW1uUYEs5zOqKso36x%2bydCc6

Thanks!

3
  • Why does it need to be changed? It sounds like you're trying to solve a symptom of a different problem entirely. Commented Oct 25, 2013 at 16:55
  • Because that information is a semi-encrypted hash and it's messing up the decryption method on the receiving end. Commented Oct 25, 2013 at 16:58
  • 3
    Maybe url-decode it and then url-encode it again? If the decoding step succeeds in producing the original string, then re-encoding it should result in the same value you originally sent. Commented Oct 25, 2013 at 17:01

1 Answer 1

4

Use preg_replace_callback and replace /%[a-zA-Z0-9]{2}/ with its uppercase equivalent using strtoupper:

5.3+:

$string = preg_replace_callback('/%[a-zA-Z0-9]{2}/', function($match) {
    return strtoupper($match[0]);
}, $string);

Less than 5.3:

function preg_replace_callback_uppercaser($match) {
    return strtoupper($match[0]);
}
$string = preg_replace_callback('/%[a-zA-Z0-9]{2}/', 'preg_replace_callback_uppercaser', $string);

Demo

<?php
    $string = '6Vc6K83k%2fnBWb6sOfc0fcQiMOttpKJci9o%2b%2bdMBs0MCGJgQkgyr6zV%2fO8hqRATKW1uUYEs5zOqKso36x%2bydCc6';

    $string = preg_replace_callback('/%[a-zA-Z0-9]{2}/', function($match) {
        return strtoupper($match[0]);
    }, $string);

    var_dump($string);
?>

Outputs:

string(96) "6Vc6K83k%2FnBWb6sOfc0fcQiMOttpKJci9o%2B%2BdMBs0MCGJgQkgyr6zV%2FO8hqRATKW1uUYEs5zOqKso36x%2BydCc6"

Link to live demo


Alternatively, David's $string = urlencode(urldecode($string)); works fine.

Sign up to request clarification or add additional context in comments.

Comments

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.