duanniling0018 2015-10-02 01:33
浏览 199

如何使用php中的utf-8字符集编码从base64解码数据

I have an xml file with dynamic contents like this.

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <tracks>
    <idx>0</idx>
    <path>/mnt/HD/HD_a2/mymusic/Worth It - Fifth Harmony - ft. Kid Ink.mp3</path>
  </tracks>
  <tracks>
    <idx>1</idx>
    <path>/mnt/HD/HD_a2/mymusic/Flashlight - Jessie J.mp3</path>
  </tracks>
  <tracks>
    <idx>2</idx>
    <path>/mnt/HD/HD_a2/mymusic/価値がある.mp3</path>
  </tracks>
</config>

And also a php file which creates another xml file according to data fetch on first xml. Here's a piece of code in generating new xml:

$item = $xml->addChild('tracks');
$item->addChild('artist', base64_encode($mp3Artist));
$item->addChild('song_name', base64_encode($mp3Title));
$item->addChild('song_path', base64_encode($file)); 
$item->addChild('album', base64_encode($mp3Album));

However, when I encode /mnt/HD/HD_a2/mymusic/価値がある.mp3

the output is L21udC9IRC9IRF9hMi9teW11c2ljL+S+oeWApOOBjOOBguOCiy5tcDM= which is when I decode it again, the value becomes /mnt/HD/HD_a2/mymusic/価値ãŒã‚ã‚‹.mp3 but when I decode it on this site https://www.base64decode.org/ with utf-8 encoding, the output is the same as the first one which is /mnt/HD/HD_a2/mymusic/価値がある.mp3

So my question is, how can I achieved utf-8 encoding on php's base64_encode to output the exactly the same as on www.base64decode.org? Any help will be much appreciated.

  • 写回答

1条回答

  • dsdapobp26141 2015-10-02 01:41
    关注

    You don't need to do anything just use base64_decode :

    $encoded = base64_encode("/mnt/HD/HD_a2/mymusic/価値がある.mp3");
    echo "\xEF\xBB\xBF" ;//Optionally add this line if you want to force a program to read as utf-8
    echo $encoded." <br />";
    $decoded = base64_decode($encoded);
    echo $decoded;
    

    And optionally add a echo "\xEF\xBB\xBF" ; on the first output line if you want to force a file to be interpreted as utf-8 by any program

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作