ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
UtfNormalUtil.php File Reference

Go to the source code of this file.

Namespaces

namespace  UtfNormal
 Unicode normalization routines for working with UTF-8 strings.
 

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
int$codepoint
Returns
string @access public

Definition at line 38 of file UtfNormalUtil.php.

38 {
39 if($codepoint < 0x80) return chr($codepoint);
40 if($codepoint < 0x800) return chr($codepoint >> 6 & 0x3f | 0xc0) .
41 chr($codepoint & 0x3f | 0x80);
42 if($codepoint < 0x10000) return chr($codepoint >> 12 & 0x0f | 0xe0) .
43 chr($codepoint >> 6 & 0x3f | 0x80) .
44 chr($codepoint & 0x3f | 0x80);
45 if($codepoint < 0x110000) return chr($codepoint >> 18 & 0x07 | 0xf0) .
46 chr($codepoint >> 12 & 0x3f | 0x80) .
47 chr($codepoint >> 6 & 0x3f | 0x80) .
48 chr($codepoint & 0x3f | 0x80);
49
50 die("Asked for code outside of range ($codepoint)\n");
51}

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
string$string
Returns
string @access public

Definition at line 136 of file UtfNormalUtil.php.

136 {
137 return strtr( $string,
138 array(
139 '\\' => '\\\\',
140 '\'' => '\\\''
141 ));
142}

◆ hexSequenceToUtf8()

hexSequenceToUtf8 (   $sequence)

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
string$sequence
Returns
string @access private

Definition at line 62 of file UtfNormalUtil.php.

62 {
63 $utf = '';
64 foreach( explode( ' ', $sequence ) as $hex ) {
65 $n = hexdec( $hex );
66 $utf .= codepointToUtf8( $n );
67 }
68 return $utf;
69}
$n
Definition: RandomTest.php:80
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
string$char
Returns
int @access public

Definition at line 96 of file UtfNormalUtil.php.

96 {
97 # Find the length
98 $z = ord( $char{0} );
99 if ( $z & 0x80 ) {
100 $length = 0;
101 while ( $z & 0x80 ) {
102 $length++;
103 $z <<= 1;
104 }
105 } else {
106 $length = 1;
107 }
108
109 if ( $length != strlen( $char ) ) {
110 return false;
111 }
112 if ( $length == 1 ) {
113 return ord( $char );
114 }
115
116 # Mask off the length-determining bits and shift back to the original location
117 $z &= 0xff;
118 $z >>= $length;
119
120 # Add in the free bits from subsequent bytes
121 for ( $i=1; $i<$length; $i++ ) {
122 $z <<= 6;
123 $z |= ord( $char{$i} ) & 0x3f;
124 }
125
126 return $z;
127}

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

+ Here is the caller graph for this function:

◆ utf8ToHexSequence()

utf8ToHexSequence (   $str)

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

For debugging.

Parameters
string$str
Returns
string @access private

Definition at line 79 of file UtfNormalUtil.php.

79 {
80 return rtrim(preg_replace_callback(
81 '/(.)/uS',
82 function($hit) {
83 return sprintf("%04x ", utf8ToCodepoint($hit[1]));
84 },
85 $str));
86}
utf8ToCodepoint( $char)
Determine the Unicode codepoint of a single-character UTF-8 sequence.

References utf8ToCodepoint().

+ Here is the call graph for this function: