如何使用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个回答

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

doulan4939
doulan4939 我刚刚找到了上述问题的解决方案,多亏了这个! stackoverflow.com/questions/1374753/...
4 年多之前 回复
dou4064
dou4064 too long
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐