1

In PHP, when url encoding using urlencode(), the outputted characters are in upper case:

echo urlencode('MyString'.chr(31));
//returns 'MyString%1F'

I need to get PHP to give me back 'MyString%1f' for the above example but not to lower case any other part of the string. in order to be consistent with other platforms. Is there any way I can do this without having to run through the string one character at a time, working out if I need to change the casing each time?

4

1 回答 1

6

你为什么要这样做呢?F或者f,它不应该有任何区别,因为百分比编码是不区分大小写的。我能想到的唯一情况是在创建哈希时,但是我个人会将整个字符串转换为大写或小写,即不区分大小写。

无论如何,如果你真的需要这样做,那么使用它应该相对容易preg_replace_callback

$original = 'MyString%1F%E2%FOO%22';
$modified = preg_replace_callback('/%[0-9A-F]{2}/', function(array $matches)
{
    return strtolower($matches[0]);
},
$original);

var_dump($modified);

这应该给你:

string(18) "MyString%1f%e2%FOO%22"
于 2013-07-03T09:59:52.043 回答