1f41bbe4cSAndreas Gohr<?php 2f41bbe4cSAndreas Gohr 3f41bbe4cSAndreas Gohrnamespace dokuwiki\Utf8; 4f41bbe4cSAndreas Gohr 5f41bbe4cSAndreas Gohr/** 6f41bbe4cSAndreas Gohr * Convert between UTF-8 and a list of Unicode Code Points 7f41bbe4cSAndreas Gohr */ 8f41bbe4cSAndreas Gohrclass Unicode 9f41bbe4cSAndreas Gohr{ 10f41bbe4cSAndreas Gohr 11f41bbe4cSAndreas Gohr /** 12f41bbe4cSAndreas Gohr * Takes an UTF-8 string and returns an array of ints representing the 13f41bbe4cSAndreas Gohr * Unicode characters. Astral planes are supported ie. the ints in the 14f41bbe4cSAndreas Gohr * output can be > 0xFFFF. Occurrances of the BOM are ignored. Surrogates 15f41bbe4cSAndreas Gohr * are not allowed. 16f41bbe4cSAndreas Gohr * 17f41bbe4cSAndreas Gohr * If $strict is set to true the function returns false if the input 18f41bbe4cSAndreas Gohr * string isn't a valid UTF-8 octet sequence and raises a PHP error at 19f41bbe4cSAndreas Gohr * level E_USER_WARNING 20f41bbe4cSAndreas Gohr * 21f41bbe4cSAndreas Gohr * Note: this function has been modified slightly in this library to 22f41bbe4cSAndreas Gohr * trigger errors on encountering bad bytes 23f41bbe4cSAndreas Gohr * 24f41bbe4cSAndreas Gohr * @author <hsivonen@iki.fi> 25f41bbe4cSAndreas Gohr * @author Harry Fuecks <hfuecks@gmail.com> 26f41bbe4cSAndreas Gohr * @see unicode_to_utf8 27f41bbe4cSAndreas Gohr * @link http://hsivonen.iki.fi/php-utf8/ 28f41bbe4cSAndreas Gohr * @link http://sourceforge.net/projects/phputf8/ 29f41bbe4cSAndreas Gohr * @todo break into less complex chunks 30f41bbe4cSAndreas Gohr * @todo use exceptions instead of user errors 31f41bbe4cSAndreas Gohr * 32f41bbe4cSAndreas Gohr * @param string $str UTF-8 encoded string 33f41bbe4cSAndreas Gohr * @param boolean $strict Check for invalid sequences? 34f41bbe4cSAndreas Gohr * @return mixed array of unicode code points or false if UTF-8 invalid 35f41bbe4cSAndreas Gohr */ 36f41bbe4cSAndreas Gohr public static function fromUtf8($str, $strict = false) 37f41bbe4cSAndreas Gohr { 38f41bbe4cSAndreas Gohr $mState = 0; // cached expected number of octets after the current octet 39f41bbe4cSAndreas Gohr // until the beginning of the next UTF8 character sequence 40f41bbe4cSAndreas Gohr $mUcs4 = 0; // cached Unicode character 41f41bbe4cSAndreas Gohr $mBytes = 1; // cached expected number of octets in the current sequence 42f41bbe4cSAndreas Gohr 43e025be72SAndreas Gohr $out = []; 44f41bbe4cSAndreas Gohr 45f41bbe4cSAndreas Gohr $len = strlen($str); 46f41bbe4cSAndreas Gohr 47f41bbe4cSAndreas Gohr for ($i = 0; $i < $len; $i++) { 482401f18dSSyntaxseed $in = ord($str[$i]); 49f41bbe4cSAndreas Gohr 50f41bbe4cSAndreas Gohr if ($mState === 0) { 51f41bbe4cSAndreas Gohr // When mState is zero we expect either a US-ASCII character or a 52f41bbe4cSAndreas Gohr // multi-octet sequence. 53f41bbe4cSAndreas Gohr if (0 === (0x80 & $in)) { 54f41bbe4cSAndreas Gohr // US-ASCII, pass straight through. 55f41bbe4cSAndreas Gohr $out[] = $in; 56f41bbe4cSAndreas Gohr $mBytes = 1; 57f41bbe4cSAndreas Gohr } elseif (0xC0 === (0xE0 & $in)) { 58f41bbe4cSAndreas Gohr // First octet of 2 octet sequence 59f41bbe4cSAndreas Gohr $mUcs4 = $in; 60f41bbe4cSAndreas Gohr $mUcs4 = ($mUcs4 & 0x1F) << 6; 61f41bbe4cSAndreas Gohr $mState = 1; 62f41bbe4cSAndreas Gohr $mBytes = 2; 63f41bbe4cSAndreas Gohr } elseif (0xE0 === (0xF0 & $in)) { 64f41bbe4cSAndreas Gohr // First octet of 3 octet sequence 65f41bbe4cSAndreas Gohr $mUcs4 = $in; 66f41bbe4cSAndreas Gohr $mUcs4 = ($mUcs4 & 0x0F) << 12; 67f41bbe4cSAndreas Gohr $mState = 2; 68f41bbe4cSAndreas Gohr $mBytes = 3; 69f41bbe4cSAndreas Gohr } elseif (0xF0 === (0xF8 & $in)) { 70f41bbe4cSAndreas Gohr // First octet of 4 octet sequence 71f41bbe4cSAndreas Gohr $mUcs4 = $in; 72f41bbe4cSAndreas Gohr $mUcs4 = ($mUcs4 & 0x07) << 18; 73f41bbe4cSAndreas Gohr $mState = 3; 74f41bbe4cSAndreas Gohr $mBytes = 4; 75f41bbe4cSAndreas Gohr } elseif (0xF8 === (0xFC & $in)) { 76f41bbe4cSAndreas Gohr /* First octet of 5 octet sequence. 77f41bbe4cSAndreas Gohr * 78f41bbe4cSAndreas Gohr * This is illegal because the encoded codepoint must be either 79f41bbe4cSAndreas Gohr * (a) not the shortest form or 80f41bbe4cSAndreas Gohr * (b) outside the Unicode range of 0-0x10FFFF. 81f41bbe4cSAndreas Gohr * Rather than trying to resynchronize, we will carry on until the end 82f41bbe4cSAndreas Gohr * of the sequence and let the later error handling code catch it. 83f41bbe4cSAndreas Gohr */ 84f41bbe4cSAndreas Gohr $mUcs4 = $in; 85f41bbe4cSAndreas Gohr $mUcs4 = ($mUcs4 & 0x03) << 24; 86f41bbe4cSAndreas Gohr $mState = 4; 87f41bbe4cSAndreas Gohr $mBytes = 5; 88f41bbe4cSAndreas Gohr } elseif (0xFC === (0xFE & $in)) { 89f41bbe4cSAndreas Gohr // First octet of 6 octet sequence, see comments for 5 octet sequence. 90f41bbe4cSAndreas Gohr $mUcs4 = $in; 91f41bbe4cSAndreas Gohr $mUcs4 = ($mUcs4 & 1) << 30; 92f41bbe4cSAndreas Gohr $mState = 5; 93f41bbe4cSAndreas Gohr $mBytes = 6; 94f41bbe4cSAndreas Gohr } elseif ($strict) { 95f41bbe4cSAndreas Gohr /* Current octet is neither in the US-ASCII range nor a legal first 96f41bbe4cSAndreas Gohr * octet of a multi-octet sequence. 97f41bbe4cSAndreas Gohr */ 98f41bbe4cSAndreas Gohr trigger_error( 99f41bbe4cSAndreas Gohr 'utf8_to_unicode: Illegal sequence identifier ' . 100f41bbe4cSAndreas Gohr 'in UTF-8 at byte ' . $i, 101f41bbe4cSAndreas Gohr E_USER_WARNING 102f41bbe4cSAndreas Gohr ); 103f41bbe4cSAndreas Gohr return false; 104f41bbe4cSAndreas Gohr } 105e025be72SAndreas Gohr } elseif (0x80 === (0xC0 & $in)) { 106f41bbe4cSAndreas Gohr // When mState is non-zero, we expect a continuation of the multi-octet 107f41bbe4cSAndreas Gohr // sequence 108f41bbe4cSAndreas Gohr // Legal continuation. 109f41bbe4cSAndreas Gohr $shift = ($mState - 1) * 6; 110f41bbe4cSAndreas Gohr $tmp = $in; 111f41bbe4cSAndreas Gohr $tmp = ($tmp & 0x0000003F) << $shift; 112f41bbe4cSAndreas Gohr $mUcs4 |= $tmp; 113f41bbe4cSAndreas Gohr /** 114f41bbe4cSAndreas Gohr * End of the multi-octet sequence. mUcs4 now contains the final 115f41bbe4cSAndreas Gohr * Unicode codepoint to be output 116f41bbe4cSAndreas Gohr */ 117f41bbe4cSAndreas Gohr if (0 === --$mState) { 118f41bbe4cSAndreas Gohr /* 119f41bbe4cSAndreas Gohr * Check for illegal sequences and codepoints. 120f41bbe4cSAndreas Gohr */ 121f41bbe4cSAndreas Gohr // From Unicode 3.1, non-shortest form is illegal 122*7d34963bSAndreas Gohr if ( 123*7d34963bSAndreas Gohr ((2 === $mBytes) && ($mUcs4 < 0x0080)) || 124f41bbe4cSAndreas Gohr ((3 === $mBytes) && ($mUcs4 < 0x0800)) || 125f41bbe4cSAndreas Gohr ((4 === $mBytes) && ($mUcs4 < 0x10000)) || 126f41bbe4cSAndreas Gohr (4 < $mBytes) || 127f41bbe4cSAndreas Gohr // From Unicode 3.2, surrogate characters are illegal 128f41bbe4cSAndreas Gohr (($mUcs4 & 0xFFFFF800) === 0xD800) || 129f41bbe4cSAndreas Gohr // Codepoints outside the Unicode range are illegal 130*7d34963bSAndreas Gohr ($mUcs4 > 0x10FFFF) 131*7d34963bSAndreas Gohr ) { 132f41bbe4cSAndreas Gohr if ($strict) { 133f41bbe4cSAndreas Gohr trigger_error( 134f41bbe4cSAndreas Gohr 'utf8_to_unicode: Illegal sequence or codepoint ' . 135f41bbe4cSAndreas Gohr 'in UTF-8 at byte ' . $i, 136f41bbe4cSAndreas Gohr E_USER_WARNING 137f41bbe4cSAndreas Gohr ); 138f41bbe4cSAndreas Gohr 139f41bbe4cSAndreas Gohr return false; 140f41bbe4cSAndreas Gohr } 141f41bbe4cSAndreas Gohr } 142f41bbe4cSAndreas Gohr 143f41bbe4cSAndreas Gohr if (0xFEFF !== $mUcs4) { 144f41bbe4cSAndreas Gohr // BOM is legal but we don't want to output it 145f41bbe4cSAndreas Gohr $out[] = $mUcs4; 146f41bbe4cSAndreas Gohr } 147f41bbe4cSAndreas Gohr 148f41bbe4cSAndreas Gohr //initialize UTF8 cache 149f41bbe4cSAndreas Gohr $mState = 0; 150f41bbe4cSAndreas Gohr $mUcs4 = 0; 151f41bbe4cSAndreas Gohr $mBytes = 1; 152f41bbe4cSAndreas Gohr } 153f41bbe4cSAndreas Gohr } elseif ($strict) { 154f41bbe4cSAndreas Gohr /** 155f41bbe4cSAndreas Gohr *((0xC0 & (*in) != 0x80) && (mState != 0)) 156f41bbe4cSAndreas Gohr * Incomplete multi-octet sequence. 157f41bbe4cSAndreas Gohr */ 158f41bbe4cSAndreas Gohr trigger_error( 159f41bbe4cSAndreas Gohr 'utf8_to_unicode: Incomplete multi-octet ' . 160f41bbe4cSAndreas Gohr ' sequence in UTF-8 at byte ' . $i, 161f41bbe4cSAndreas Gohr E_USER_WARNING 162f41bbe4cSAndreas Gohr ); 163f41bbe4cSAndreas Gohr 164f41bbe4cSAndreas Gohr return false; 165f41bbe4cSAndreas Gohr } 166f41bbe4cSAndreas Gohr } 167f41bbe4cSAndreas Gohr return $out; 168f41bbe4cSAndreas Gohr } 169f41bbe4cSAndreas Gohr 170f41bbe4cSAndreas Gohr /** 171f41bbe4cSAndreas Gohr * Takes an array of ints representing the Unicode characters and returns 172f41bbe4cSAndreas Gohr * a UTF-8 string. Astral planes are supported ie. the ints in the 173f41bbe4cSAndreas Gohr * input can be > 0xFFFF. Occurrances of the BOM are ignored. Surrogates 174f41bbe4cSAndreas Gohr * are not allowed. 175f41bbe4cSAndreas Gohr * 176f41bbe4cSAndreas Gohr * If $strict is set to true the function returns false if the input 177f41bbe4cSAndreas Gohr * array contains ints that represent surrogates or are outside the 178f41bbe4cSAndreas Gohr * Unicode range and raises a PHP error at level E_USER_WARNING 179f41bbe4cSAndreas Gohr * 180f41bbe4cSAndreas Gohr * Note: this function has been modified slightly in this library to use 181f41bbe4cSAndreas Gohr * output buffering to concatenate the UTF-8 string (faster) as well as 182f41bbe4cSAndreas Gohr * reference the array by it's keys 183f41bbe4cSAndreas Gohr * 184f41bbe4cSAndreas Gohr * @param array $arr of unicode code points representing a string 185f41bbe4cSAndreas Gohr * @param boolean $strict Check for invalid sequences? 186f41bbe4cSAndreas Gohr * @return string|false UTF-8 string or false if array contains invalid code points 187f41bbe4cSAndreas Gohr * 188f41bbe4cSAndreas Gohr * @author <hsivonen@iki.fi> 189f41bbe4cSAndreas Gohr * @author Harry Fuecks <hfuecks@gmail.com> 190f41bbe4cSAndreas Gohr * @see utf8_to_unicode 191f41bbe4cSAndreas Gohr * @link http://hsivonen.iki.fi/php-utf8/ 192f41bbe4cSAndreas Gohr * @link http://sourceforge.net/projects/phputf8/ 193f41bbe4cSAndreas Gohr * @todo use exceptions instead of user errors 194f41bbe4cSAndreas Gohr */ 195f41bbe4cSAndreas Gohr public static function toUtf8($arr, $strict = false) 196f41bbe4cSAndreas Gohr { 197f41bbe4cSAndreas Gohr if (!is_array($arr)) return ''; 198f41bbe4cSAndreas Gohr ob_start(); 199f41bbe4cSAndreas Gohr 200f41bbe4cSAndreas Gohr foreach (array_keys($arr) as $k) { 201f41bbe4cSAndreas Gohr if (($arr[$k] >= 0) && ($arr[$k] <= 0x007f)) { 202f41bbe4cSAndreas Gohr # ASCII range (including control chars) 203f41bbe4cSAndreas Gohr echo chr($arr[$k]); 204f41bbe4cSAndreas Gohr } elseif ($arr[$k] <= 0x07ff) { 205f41bbe4cSAndreas Gohr # 2 byte sequence 206f41bbe4cSAndreas Gohr echo chr(0xc0 | ($arr[$k] >> 6)); 207f41bbe4cSAndreas Gohr echo chr(0x80 | ($arr[$k] & 0x003f)); 208f41bbe4cSAndreas Gohr } elseif ($arr[$k] == 0xFEFF) { 209f41bbe4cSAndreas Gohr # Byte order mark (skip) 210f41bbe4cSAndreas Gohr // nop -- zap the BOM 211f41bbe4cSAndreas Gohr } elseif ($arr[$k] >= 0xD800 && $arr[$k] <= 0xDFFF) { 212f41bbe4cSAndreas Gohr # Test for illegal surrogates 213f41bbe4cSAndreas Gohr // found a surrogate 214f41bbe4cSAndreas Gohr if ($strict) { 215f41bbe4cSAndreas Gohr trigger_error( 216f41bbe4cSAndreas Gohr 'unicode_to_utf8: Illegal surrogate ' . 217f41bbe4cSAndreas Gohr 'at index: ' . $k . ', value: ' . $arr[$k], 218f41bbe4cSAndreas Gohr E_USER_WARNING 219f41bbe4cSAndreas Gohr ); 220f41bbe4cSAndreas Gohr return false; 221f41bbe4cSAndreas Gohr } 222f41bbe4cSAndreas Gohr } elseif ($arr[$k] <= 0xffff) { 223f41bbe4cSAndreas Gohr # 3 byte sequence 224f41bbe4cSAndreas Gohr echo chr(0xe0 | ($arr[$k] >> 12)); 225f41bbe4cSAndreas Gohr echo chr(0x80 | (($arr[$k] >> 6) & 0x003f)); 226f41bbe4cSAndreas Gohr echo chr(0x80 | ($arr[$k] & 0x003f)); 227f41bbe4cSAndreas Gohr } elseif ($arr[$k] <= 0x10ffff) { 228f41bbe4cSAndreas Gohr # 4 byte sequence 229f41bbe4cSAndreas Gohr echo chr(0xf0 | ($arr[$k] >> 18)); 230f41bbe4cSAndreas Gohr echo chr(0x80 | (($arr[$k] >> 12) & 0x3f)); 231f41bbe4cSAndreas Gohr echo chr(0x80 | (($arr[$k] >> 6) & 0x3f)); 232f41bbe4cSAndreas Gohr echo chr(0x80 | ($arr[$k] & 0x3f)); 233f41bbe4cSAndreas Gohr } elseif ($strict) { 234f41bbe4cSAndreas Gohr trigger_error( 235f41bbe4cSAndreas Gohr 'unicode_to_utf8: Codepoint out of Unicode range ' . 236f41bbe4cSAndreas Gohr 'at index: ' . $k . ', value: ' . $arr[$k], 237f41bbe4cSAndreas Gohr E_USER_WARNING 238f41bbe4cSAndreas Gohr ); 239f41bbe4cSAndreas Gohr 240f41bbe4cSAndreas Gohr // out of range 241f41bbe4cSAndreas Gohr return false; 242f41bbe4cSAndreas Gohr } 243f41bbe4cSAndreas Gohr } 244f41bbe4cSAndreas Gohr 245f41bbe4cSAndreas Gohr return ob_get_clean(); 246f41bbe4cSAndreas Gohr } 247f41bbe4cSAndreas Gohr} 248