我使用下面的代码简单地解码然后重新编码(相对较长的)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";
然后我注意到两个解码的字符串长度相同。但我需要知道为什么原始编码字符串和重新编码的字符串长度不同以及如何防止它。
答案 0 :(得分:1)
重新编码的字符串有两种不同的做法:
=
正在重新编码为%3D
,使您的字符串变长2个字符。
%20
正在重新编码为+
,使字符串缩短8个字符(4次出现)
净差异是你看到的6个字符。
做一个简单的str_replace,比如
$encoded = str_replace(["%3D", "+"], ["=", "%20"], $encoded);
在这种情况下应解决问题,但两者都是编码的有效表示,我很好奇为什么长度差异是一个问题。