简体   繁体   中英

Decode base64 string - php

Is there any way to decode this string??

Actual string : 其他語言測試 - testing

base64 encode while sending on mail as subject as

"=?iso-2022-jp?B?GyRCQjZCPjhsOEBCLDtuGyhCIC0gdGVzdGluZw==?="

<?php
echo base64_decode('GyRCQjZCPjhsOEBCLDtuGyhCIC0gdGVzdGluZw==');
?>

This is base 64 encode, I couldn't decode it to actual Chinese string.Since it has been encoded using "iso-2022-jp", I have also tried online base64decode.org site to decode this string, but I couldn't find the original string, how can I do that?

Use iconv() :

<?php
$input = base64_decode('GyRCQjZCPjhsOEBCLDtuGyhCIC0gdGVzdGluZw==');//$BB6B>8l8@B,;n(B - testing
$input_encoding = 'iso-2022-jp';
echo iconv($input_encoding, 'UTF-8', $input); //其他語言測試 - testing
?>

What you are looking at is MIME header encoding. It can be decoded by mb_decode_mimeheader() , and generated by mb_encode_mimeheader() . For example:

<?php
mb_internal_encoding("utf-8");
$subj = "=?iso-2022-jp?B?GyRCQjZCPjhsOEBCLDtuGyhCIC0gdGVzdGluZw==?=";
print mb_decode_mimeheader($subj);
?>

其他語言測試 - testing

(The call to mb_internal_encoding() is necessary here because the contents of the subject line can't be represented in the default internal encoding of ISO8859-1.)

Try encoding the string to UTF-8 first and then encode it to base 64. Same when decoding, decode the string from base64 and then from UTF-8. This is working for me:

php > $base = "其他語言測試 - testing";
php > $encoded = base64_encode(utf8_encode($base));
php > $decoded = utf8_decode(base64_decode($encoded));
php > echo ($decoded === $base) . "\n";
1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM