Repository navigation
Expand file tree
/
Copy pathUtfString.php
More file actions
69 lines (57 loc) · 1.4 KB
/
Copy pathUtfString.php
File metadata and controls
69 lines (57 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace Mpdf\Utils;
class UtfString
{
/**
* Converts all the &#nnn; and &#xhhh; in a string to Unicode
*
* @since mPDF 5.7
* @param string $str
* @param bool $lo
*
* @return string
*/
public static function strcode2utf($str, $lo = true)
{
$str = preg_replace_callback('/\&\#(\d+)\;/m', function ($matches) use ($lo) {
return static::code2utf($matches[1], $lo ? 1 : 0);
}, $str);
$str = preg_replace_callback('/\&\#x([0-9a-fA-F]+)\;/m', function ($matches) use ($lo) {
return static::codeHex2utf($matches[1], $lo ? 1 : 0);
}, $str);
return $str;
}
/**
* @param int $num
* @param bool $lo
*
* @return string
*/
public static function code2utf($num, $lo = true)
{
// Returns the utf string corresponding to the unicode value
if ($num < 128) {
return $lo
? chr($num)
: '&#' . $num . ';';
}
if ($num < 2048) {
return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
}
if ($num < 65536) {
return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
}
if ($num < 2097152) {
return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
}
return '?';
}
public static function codeHex2utf($hex, $lo = true)
{
$num = hexdec($hex);
if (($num < 128) && !$lo) {
return '&#x' . $hex . ';';
}
return static::code2utf($num, $lo);
}
}