How to convert UTF-8 byte[] to string in PHP?
So if you have an array of bytes and want to turn that into a PHP string in order to convert the character set using
mb_convert_encoding()
, try the following:$input = array(0x53, 0x68, 0x69);
$output = '';
for ($i = 0, $j = count($input); $i < $j; ++$i) {
$output .= chr($input[$i]);
}
$output_utf8 = mb_convert_encoding($output, 'utf-8', 'enter input encoding here');
utf8_encode
<?php
// Convert the string 'Zoƫ' from ISO 8859-1 to UTF-8
$iso8859_1_string = "\x5A\x6F\xEB";
$utf8_string = utf8_encode($iso8859_1_string);
echo bin2hex($utf8_string), "\n";
?>
Output
5a6fc3ab