How to decode data from base64 with utf-8 charset encoding in php
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.
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