function encodeUTF8($array) { foreach ($array as $key => $value) { if (!is_array($value)) { $array[$key] = mb_convert_encoding($value, "UTF-8", "GBK"); } else { encodeUTF8($array[$key]); } } return $array; }
function encodeBase64($str) { $i = 0; $enc = ''; $str = encodeUTF8($str); // encode multi-byte chars into UTF-8 for byte-array do { // pack three octets into four hexets $o1 = ord($str[$i++]); $o2 = ord($str[$i++]); $o3 = ord($str[$i++]); $bits = $o1 << 16 | $o2 << 8 | $o3; $h1 = $bits >> 18 & 0x3f; $h2 = $bits >> 12 & 0x3f; $h3 = $bits >> 6 & 0x3f; $h4 = $bits & 0x3f; // end of string? index to '=' in b64 if (is_nan($o3)) { $h4 = 64; } if (is_nan($o2)) { $h3 = 64; } // use hexets to index into b64, and append result to encoded string $enc += $b64[$h1] + $b64[$h2] + $b64[$h3] + $b64[$h4]; } while ($i < strlen($str)); return $enc; }