9 * Converts all the &#nnn; and &#xhhh; in a string to Unicode
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);
22 $str = preg_replace_callback('/\&\#x([0-9a-fA-F]+)\;/m', function ($matches) use ($lo) {
23 return static::codeHex2utf($matches[1], $lo ?
1 : 0);
35 public static function code2utf($num, $lo = true)
37 // Returns the utf string corresponding to the unicode value
42 return '&#' . $num . ';';
45 return chr(($num >> 6) +
192) . chr(($num & 63) +
128);
48 return chr(($num >> 12) +
224) . chr((($num >> 6) & 63) +
128) . chr(($num & 63) +
128);
51 return chr(($num >> 18) +
240) . chr((($num >> 12) & 63) +
128) . chr((($num >> 6) & 63) +
128) . chr(($num & 63) +
128);
57 public static function codeHex2utf($hex, $lo = true)
60 if (($num < 128) && !$lo) {
61 return '&#x' . $hex . ';';
64 return static::code2utf($num, $lo);