Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

Utf8Converter.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Persistence;
8
9 use RuntimeException;
10
17 class Utf8Converter
18 {
26 public static function toUTF8Character($charCode)
27 {
28 switch ($charCode) {
29 case 0:
30 $char = chr(0);
31 break;
32 case !($charCode & 0xffffff80): // 7 bit
33 $char = chr($charCode);
34 break;
35 case !($charCode & 0xfffff800): // 11 bit
36 $char = (
37 chr(0xc0 | (($charCode >> 6) & 0x1f)) .
38 chr(0x80 | ($charCode & 0x3f))
39 );
40 break;
41 case !($charCode & 0xffff0000): // 16 bit
42 $char = (
43 chr(0xe0 | (($charCode >> 12) & 0x0f)) .
44 chr(0x80 | (($charCode >> 6) & 0x3f)) .
45 chr(0x80 | ($charCode & 0x3f))
46 );
47 break;
48 case !($charCode & 0xffe00000): // 21 bit
49 $char = (
50 chr(0xf0 | (($charCode >> 18) & 0x07)) .
51 chr(0x80 | (($charCode >> 12) & 0x3f)) .
52 chr(0x80 | (($charCode >> 6) & 0x3f)) .
53 chr(0x80 | ($charCode & 0x3f))
54 );
55 break;
56 case !($charCode & 0xfc000000): // 26 bit
57 $char = (
58 chr(0xf8 | (($charCode >> 24) & 0x03)) .
59 chr(0x80 | (($charCode >> 18) & 0x3f)) .
60 chr(0x80 | (($charCode >> 12) & 0x3f)) .
61 chr(0x80 | (($charCode >> 6) & 0x3f)) .
62 chr(0x80 | ($charCode & 0x3f))
63 );
64 break;
65 default: // 31 bit
66 $char = (
67 chr(0xfc | (($charCode >> 30) & 0x01)) .
68 chr(0x80 | (($charCode >> 24) & 0x3f)) .
69 chr(0x80 | (($charCode >> 18) & 0x3f)) .
70 chr(0x80 | (($charCode >> 12) & 0x3f)) .
71 chr(0x80 | (($charCode >> 6) & 0x3f)) .
72 chr(0x80 | ($charCode & 0x3f))
73 );
74 }
75
76 return $char;
77 }
78
86 public static function toUnicodeCodepoint($char)
87 {
88 $charCode = false;
89 // 7bits, 1 char
90 if ((ord($char[0]) & 0x80) == 0x00) {
91 $charCode = ord($char[0]);
92 } elseif ((ord($char[0]) & 0xe0) == 0xc0) { // 11 bits, 2 chars
93 if ((ord($char[1]) & 0xc0) != 0x80) {
94 return $charCode;
95 }
96
97 $charCode = (
98 ((ord($char[0]) & 0x1f) << 6) +
99 ((ord($char[1]) & 0x3f))
100 );
101
102 if ($charCode < 128) {
103 throw new RuntimeException('Illegal UTF-8 input character: ' . $char);
104 }
105 } elseif ((ord($char[0]) & 0xf0) == 0xe0) { // 16 bits, 3 chars
106 if ((ord($char[1]) & 0xc0) != 0x80 ||
107 (ord($char[2]) & 0xc0) != 0x80) {
108 return $charCode;
109 }
110 $charCode = (
111 ((ord($char[0]) & 0x0f) << 12) +
112 ((ord($char[1]) & 0x3f) << 6) +
113 ((ord($char[2]) & 0x3f))
114 );
115
116 if ($charCode < 2048) {
117 throw new RuntimeException('Illegal UTF-8 input character: ' . $char);
118 }
119 } elseif ((ord($char[0]) & 0xf8) == 0xf0) { // 21 bits, 4 chars
120 if ((ord($char[1]) & 0xc0) != 0x80 ||
121 (ord($char[2]) & 0xc0) != 0x80 ||
122 (ord($char[3]) & 0xc0) != 0x80) {
123 return $charCode;
124 }
125
126 $charCode = (
127 ((ord($char[0]) & 0x07) << 18) +
128 ((ord($char[1]) & 0x3f) << 12) +
129 ((ord($char[2]) & 0x3f) << 6) +
130 ((ord($char[3]) & 0x3f))
131 );
132
133 if ($charCode < 65536) {
134 throw new RuntimeException('Illegal UTF-8 input character: ' . $char);
135 }
136 } elseif ((ord($char[0]) & 0xfc) == 0xf8) { // 26 bits, 5 chars
137 if ((ord($char[1]) & 0xc0) != 0x80 ||
138 (ord($char[2]) & 0xc0) != 0x80 ||
139 (ord($char[3]) & 0xc0) != 0x80 ||
140 (ord($char[4]) & 0xc0) != 0x80) {
141 return $charCode;
142 }
143
144 $charCode = (
145 ((ord($char[0]) & 0x03) << 24) +
146 ((ord($char[1]) & 0x3f) << 18) +
147 ((ord($char[2]) & 0x3f) << 12) +
148 ((ord($char[3]) & 0x3f) << 6) +
149 ((ord($char[4]) & 0x3f))
150 );
151
152 if ($charCode < 2097152) {
153 throw new RuntimeException('Illegal UTF-8 input character: ' . $char);
154 }
155 } elseif ((ord($char[0]) & 0xfe) == 0xfc) { // 31 bits, 6 chars
156 if ((ord($char[1]) & 0xc0) != 0x80 ||
157 (ord($char[2]) & 0xc0) != 0x80 ||
158 (ord($char[3]) & 0xc0) != 0x80 ||
159 (ord($char[4]) & 0xc0) != 0x80 ||
160 (ord($char[5]) & 0xc0) != 0x80) {
161 return $charCode;
162 }
163
164 $charCode = (
165 ((ord($char[0]) & 0x01) << 30) +
166 ((ord($char[1]) & 0x3f) << 24) +
167 ((ord($char[2]) & 0x3f) << 18) +
168 ((ord($char[3]) & 0x3f) << 12) +
169 ((ord($char[4]) & 0x3f) << 6) +
170 ((ord($char[5]) & 0x3f))
171 );
172
173 if ($charCode < 67108864) {
174 throw new RuntimeException('Illegal UTF-8 input character: ' . $char);
175 }
176 }
177
178 return $charCode;
179 }
180 }
eZ\Publish\Core\Persistence\Utf8Converter
Class for converting UTF-8 characters to their decimal code points and vice versa.
Definition: Utf8Converter.php:18
eZ\Publish\Core\Persistence\Utf8Converter\toUnicodeCodepoint
static toUnicodeCodepoint($char)
Convert a single UTF-8 character into its decimal code point.
Definition: Utf8Converter.php:86
eZ\Publish\Core\Persistence\Utf8Converter\toUTF8Character
static toUTF8Character($charCode)
Convert character code to UTF-8 character.
Definition: Utf8Converter.php:26
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.