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