Merge branch '3.2/develop' of http://github.com/kohana/core into 3.2/develop
[kohana-core.git] / utf8 / from_unicode.php
blob620d152f7542c74690cc526591d720a4c6ebb1c7
1 <?php defined('SYSPATH') or die('No direct script access.');
2 /**
3 * UTF8::from_unicode
5 * @package Kohana
6 * @author Kohana Team
7 * @copyright (c) 2007-2011 Kohana Team
8 * @copyright (c) 2005 Harry Fuecks
9 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
11 function _from_unicode($arr)
13 ob_start();
15 $keys = array_keys($arr);
17 foreach ($keys as $k)
19 // ASCII range (including control chars)
20 if (($arr[$k] >= 0) AND ($arr[$k] <= 0x007f))
22 echo chr($arr[$k]);
24 // 2 byte sequence
25 elseif ($arr[$k] <= 0x07ff)
27 echo chr(0xc0 | ($arr[$k] >> 6));
28 echo chr(0x80 | ($arr[$k] & 0x003f));
30 // Byte order mark (skip)
31 elseif ($arr[$k] == 0xFEFF)
33 // nop -- zap the BOM
35 // Test for illegal surrogates
36 elseif ($arr[$k] >= 0xD800 AND $arr[$k] <= 0xDFFF)
38 // Found a surrogate
39 throw new UTF8_Exception("UTF8::from_unicode: Illegal surrogate at index: ':index', value: ':value'", array(
40 ':index' => $k,
41 ':value' => $arr[$k],
42 ));
44 // 3 byte sequence
45 elseif ($arr[$k] <= 0xffff)
47 echo chr(0xe0 | ($arr[$k] >> 12));
48 echo chr(0x80 | (($arr[$k] >> 6) & 0x003f));
49 echo chr(0x80 | ($arr[$k] & 0x003f));
51 // 4 byte sequence
52 elseif ($arr[$k] <= 0x10ffff)
54 echo chr(0xf0 | ($arr[$k] >> 18));
55 echo chr(0x80 | (($arr[$k] >> 12) & 0x3f));
56 echo chr(0x80 | (($arr[$k] >> 6) & 0x3f));
57 echo chr(0x80 | ($arr[$k] & 0x3f));
59 // Out of range
60 else
62 throw new UTF8_Exception("UTF8::from_unicode: Codepoint out of Unicode range at index: ':index', value: ':value'", array(
63 ':index' => $k,
64 ':value' => $arr[$k],
65 ));
69 $result = ob_get_contents();
70 ob_end_clean();
71 return $result;