duansao20000508 2013-09-11 03:04
浏览 86
已采纳

解码后再更改长度,然后重新编码URL编码的字符串

I'm using the code below to simply decode and then re-encode a (relatively long) URL encoded string, without ever modifying the contents of the string:

<?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";

?>

The original string is 930 chars long. After decoding and re-encoding, it's down to 924 chars. Why is this happening and how can I prevent it?

EDIT:

It should be noted that if I decode $encoded as such:

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

Then I notice that both decoded strings are of the same length. But I need to know why the original encoded string and the re-encoded string are of different length and how I can prevent that.

  • 写回答

1条回答 默认 最新

  • douningchang3610 2013-09-11 03:43
    关注

    The reencoded string is doing two things differently:

    = is being reencoded to %3D making your string 2 characters longer.

    %20 is being reencoded as + making the string 8 characters shorter (4 occurances)

    The net difference is the 6 characters you are seeing.

    Doing a simple str_replace like

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

    Should resolve the problem in this case but both are valid representations of the encoding, I am curious as to why the length difference is a problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了