Takes an array of ints representing the Unicode characters and returns a UTF-8 string. Astral planes are supported i.e. the ints in the input can be > 0xFFFF. Occurrances of the BOM are ignored. Surrogates are not allowed.
$str = UTF8::to_unicode($array);
The Original Code is Mozilla Communicator client code. The Initial Developer of the Original Code is Netscape Communications Corporation. Portions created by the Initial Developer are Copyright (C) 1998 the Initial Developer. Ported to PHP by Henri Sivonen hsivonen@iki.fi, see http://hsivonen.iki.fi/php-utf8/ Slight modifications to fit with phputf8 library by Harry Fuecks hfuecks@gmail.com.
string Core_UTF8::from_unicode( array $arr )
参数列表
参数 类型 描述 默认值 $arr
array
Unicode code points representing a string
string
utf8 string of charactersboolean
FALSE if a code point cannot be foundpublic static function from_unicode($arr)
{
ob_start();
$keys = array_keys($arr);
foreach ( $keys as $k )
{
// ASCII range (including control chars)
if ( ($arr[$k] >= 0) and ($arr[$k] <= 0x007f) )
{
echo chr($arr[$k]);
}
// 2 byte sequence
elseif ( $arr[$k] <= 0x07ff )
{
echo chr(0xc0 | ($arr[$k] >> 6));
echo chr(0x80 | ($arr[$k] & 0x003f));
}
// Byte order mark (skip)
elseif ( $arr[$k] == 0xFEFF )
{
// nop -- zap the BOM
}
// Test for illegal surrogates
elseif ( $arr[$k] >= 0xD800 and $arr[$k] <= 0xDFFF )
{
// Found a surrogate
trigger_error('UTF8::from_unicode: Illegal surrogate at index: ' . $k . ', value: ' . $arr[$k], E_USER_WARNING);
return FALSE;
}
// 3 byte sequence
elseif ( $arr[$k] <= 0xffff )
{
echo chr(0xe0 | ($arr[$k] >> 12));
echo chr(0x80 | (($arr[$k] >> 6) & 0x003f));
echo chr(0x80 | ($arr[$k] & 0x003f));
}
// 4 byte sequence
elseif ( $arr[$k] <= 0x10ffff )
{
echo chr(0xf0 | ($arr[$k] >> 18));
echo chr(0x80 | (($arr[$k] >> 12) & 0x3f));
echo chr(0x80 | (($arr[$k] >> 6) & 0x3f));
echo chr(0x80 | ($arr[$k] & 0x3f));
}
// Out of range
else
{
trigger_error('UTF8::from_unicode: Codepoint out of Unicode range at index: ' . $k . ', value: ' . $arr[$k], E_USER_WARNING);
return FALSE;
}
}
$result = ob_get_contents();
ob_end_clean();
return $result;
}