0

我使用下面的代码简单地解码然后重新编码一个(相对较长的)URL 编码字符串,而无需修改字符串的内容:

<?php

$string = 'Data=%7B%22Data%22%3A%22%5B%7B%5C%22RoomId%5C%22%3A%5C%225241306%5C%22%2C%5C%22ControlName%5C%22%3A%5C%221%3A0%3Arate%5C%22%2C%5C%22Rate%5C%22%3A%5C%22RACK%20RATE%5C%22%2C%5C%22Allocation%5C%22%3A%5C%222%5C%22%2C%5C%22Status%5C%22%3A%5C%22a%5C%22%2C%5C%22MinStay%5C%22%3A%5C%221%5C%22%2C%5C%22ErrorControlId%5C%22%3A%5C%221%3A0%3Arate%5C%22%2C%5C%22AllocationDate%5C%22%3A%5C%2216%2F09%2F2013%2000%3A00%3A00%5C%22%7D%2C%7B%5C%22RoomId%5C%22%3A%5C%225241306%5C%22%2C%5C%22ControlName%5C%22%3A%5C%220%3A0%3Arate%5C%22%2C%5C%22Rate%5C%22%3A%5C%22RACK%20RATE%5C%22%2C%5C%22Allocation%5C%22%3A%5C%221%5C%22%2C%5C%22Status%5C%22%3A%5C%22a%5C%22%2C%5C%22MinStay%5C%22%3A%5C%221%5C%22%2C%5C%22ErrorControlId%5C%22%3A%5C%220%3A0%3Arate%5C%22%2C%5C%22AllocationDate%5C%22%3A%5C%2215%2F09%2F2013%2000%3A00%3A00%5C%22%7D%5D%22%2C%22IsWizard%22%3Afalse%2C%22InitialBindDate%22%3A%2215%2F09%2F13%22%2C%22EndBindDate%22%3A%2217%2F09%2F2013%22%7D';
echo $string;

$decoded = urldecode($string);
echo "<br><br>$decoded";

$encoded = urlencode($decoded);
echo "<br><br>$encoded";

?>

原始字符串长 930 个字符。解码和重新编码后,它减少到 924 个字符。为什么会发生这种情况,我该如何预防?

编辑:

应该注意的是,如果我这样解码 $encoded :

$decodedTwo = urldecode($encoded);
echo "<br><br>$decodedTwo";

然后我注意到两个解码字符串的长度相同。但我需要知道为什么原始编码字符串和重新编码字符串的长度不同,以及如何防止这种情况。

4

1 回答 1

1

重新编码的字符串有两件不同的事情:

=正在重新编码以%3D使您的字符串 2 个字符更长。

%20正在重新编码为+使字符串缩短 8 个字符(4 次出现)

净差异是您看到的 6 个字符。

做一个简单的 str_replace 像

$encoded = str_replace(["%3D", "+"], ["=", "%20"], $encoded);

在这种情况下应该解决问题,但两者都是编码的有效表示,我很好奇为什么长度差异是一个问题。

于 2013-09-11T03:43:05.140 回答