ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
UtfNormalUtil.php File Reference

Go to the source code of this file.

Functions

 codepointToUtf8 ($codepoint)
 Return UTF-8 sequence for a given Unicode code point. More...
 
 hexSequenceToUtf8 ($sequence)
 Take a series of space-separated hexadecimal numbers representing Unicode code points and return a UTF-8 string composed of those characters. More...
 
 utf8ToHexSequence ($str)
 Take a UTF-8 string and return a space-separated series of hex numbers representing Unicode code points. More...
 
 utf8ToCodepoint ($char)
 Determine the Unicode codepoint of a single-character UTF-8 sequence. More...
 
 escapeSingleString ($string)
 Escape a string for inclusion in a PHP single-quoted string literal. More...
 

Function Documentation

◆ codepointToUtf8()

codepointToUtf8 (   $codepoint)

Return UTF-8 sequence for a given Unicode code point.

May die if fed out of range data.

Parameters
$codepointInteger:
Returns
String

Definition at line 38 of file UtfNormalUtil.php.

39{
40 if ($codepoint < 0x80) {
41 return chr($codepoint);
42 }
43 if ($codepoint < 0x800) {
44 return chr($codepoint >> 6 & 0x3f | 0xc0) .
45 chr($codepoint & 0x3f | 0x80);
46 }
47 if ($codepoint < 0x10000) {
48 return chr($codepoint >> 12 & 0x0f | 0xe0) .
49 chr($codepoint >> 6 & 0x3f | 0x80) .
50 chr($codepoint & 0x3f | 0x80);
51 }
52 if ($codepoint < 0x110000) {
53 return chr($codepoint >> 18 & 0x07 | 0xf0) .
54 chr($codepoint >> 12 & 0x3f | 0x80) .
55 chr($codepoint >> 6 & 0x3f | 0x80) .
56 chr($codepoint & 0x3f | 0x80);
57 }
58
59 echo "Asked for code outside of range ($codepoint)\n";
60 die(-1);
61}

Referenced by ilCharSelectorConfig\getItemParsedCallback(), hexSequenceToUtf8(), and CleanUpTest\XtestAllChars().

+ Here is the caller graph for this function:

◆ escapeSingleString()

escapeSingleString (   $string)

Escape a string for inclusion in a PHP single-quoted string literal.

Parameters
$stringString: string to be escaped.
Returns
String: escaped string.

Definition at line 150 of file UtfNormalUtil.php.

151{
152 return strtr(
153 $string,
154 array(
155 '\\' => '\\\\',
156 '\'' => '\\\''
157 )
158 );
159}

◆ hexSequenceToUtf8()

hexSequenceToUtf8 (   $sequence)
private

Take a series of space-separated hexadecimal numbers representing Unicode code points and return a UTF-8 string composed of those characters.

Used by UTF-8 data generation and testing routines.

Parameters
$sequenceString
Returns
String

Definition at line 72 of file UtfNormalUtil.php.

73{
74 $utf = '';
75 foreach (explode(' ', $sequence) as $hex) {
76 $n = hexdec($hex);
77 $utf .= codepointToUtf8($n);
78 }
79 return $utf;
80}
$n
Definition: RandomTest.php:85
codepointToUtf8($codepoint)
Return UTF-8 sequence for a given Unicode code point.

References $n, and codepointToUtf8().

+ Here is the call graph for this function:

◆ utf8ToCodepoint()

utf8ToCodepoint (   $char)

Determine the Unicode codepoint of a single-character UTF-8 sequence.

Does not check for invalid input data.

Parameters
$charString
Returns
Integer

Definition at line 109 of file UtfNormalUtil.php.

110{
111 # Find the length
112 $z = ord($char[0]);
113 if ($z & 0x80) {
114 $length = 0;
115 while ($z & 0x80) {
116 $length++;
117 $z <<= 1;
118 }
119 } else {
120 $length = 1;
121 }
122
123 if ($length != strlen($char)) {
124 return false;
125 }
126 if ($length == 1) {
127 return ord($char);
128 }
129
130 # Mask off the length-determining bits and shift back to the original location
131 $z &= 0xff;
132 $z >>= $length;
133
134 # Add in the free bits from subsequent bytes
135 for ($i = 1; $i < $length; $i++) {
136 $z <<= 6;
137 $z |= ord($char[$i]) & 0x3f;
138 }
139
140 return $z;
141}
$i
Definition: disco.tpl.php:19

References $i.

Referenced by ilCharSelectorConfig\getItemCodepoint(), and utf8ToHexSequence().

+ Here is the caller graph for this function:

◆ utf8ToHexSequence()

utf8ToHexSequence (   $str)
private

Take a UTF-8 string and return a space-separated series of hex numbers representing Unicode code points.

For debugging.

Parameters
$strString: UTF-8 string.
Returns
string

Definition at line 90 of file UtfNormalUtil.php.

91{
92 return rtrim(preg_replace_callback(
93 '/(.)/uS',
94 function ($hit) {
95 return sprintf("%04x ", utf8ToCodepoint($hit[1]));
96 },
97 $str
98 ));
99}
utf8ToCodepoint($char)
Determine the Unicode codepoint of a single-character UTF-8 sequence.

References utf8ToCodepoint().

+ Here is the call graph for this function: