composer package updates
[openemr.git] / vendor / mpdf / mpdf / src / Utils / UtfString.php
blob8b5c8c940cdbcd44105024cb8daffc597e046104
1 <?php
3 namespace Mpdf\Utils;
5 class UtfString
8 /**
9 * Converts all the &#nnn; and &#xhhh; in a string to Unicode
11 * @since mPDF 5.7
12 * @param string $str
13 * @param bool $lo
15 * @return string
17 public static function strcode2utf($str, $lo = true)
19 $str = preg_replace_callback('/\&\#(\d+)\;/m', function ($matches) use ($lo) {
20 return static::code2utf($matches[1], $lo ? 1 : 0);
21 }, $str);
22 $str = preg_replace_callback('/\&\#x([0-9a-fA-F]+)\;/m', function ($matches) use ($lo) {
23 return static::codeHex2utf($matches[1], $lo ? 1 : 0);
24 }, $str);
26 return $str;
29 /**
30 * @param int $num
31 * @param bool $lo
33 * @return string
35 public static function code2utf($num, $lo = true)
37 // Returns the utf string corresponding to the unicode value
38 if ($num < 128) {
39 if ($lo) {
40 return chr($num);
42 return '&#' . $num . ';';
44 if ($num < 2048) {
45 return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
47 if ($num < 65536) {
48 return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
50 if ($num < 2097152) {
51 return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
54 return '?';
57 public static function codeHex2utf($hex, $lo = true)
59 $num = hexdec($hex);
60 if (($num < 128) && !$lo) {
61 return '&#x' . $hex . ';';
64 return static::code2utf($num, $lo);