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