使用preg_replace_callback
并替换/%[a-zA-Z0-9]{2}/
为大写等效项strtoupper
:
5.3+:
$string = preg_replace_callback('/%[a-zA-Z0-9]{2}/', function($match) {
return strtoupper($match[0]);
}, $string);
小于 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);
演示
<?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);
?>
输出:
字符串 (96) "6Vc6K83k%2FnBWb6sOfc0fcQiMOttpKJci9o%2B%2BdMBs0MCGJgQkgyr6zV%2FO8hqRATKW1uUYEs5zOqKso36x%2BydCc6"
链接到现场演示
或者,David的$string = urlencode(urldecode($string));
工作正常。