-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathunicode.php
More file actions
83 lines (74 loc) · 1.78 KB
/
Copy pathunicode.php
File metadata and controls
83 lines (74 loc) · 1.78 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
*
* This file is part of the phpBB Customisation Database package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\titania;
class unicode
{
/**
* Escape four-byte UTF-8 characters as JSON surrogate pairs.
*
* This leaves the structure and formatting of the JSON document intact.
*
* @param string $json
* @return string
*/
public static function escape_json($json)
{
$escaped_json = preg_replace_callback(
'/[\x{10000}-\x{10FFFF}]/u',
function ($matches)
{
$codepoint = utf8_ord($matches[0]) - 0x10000;
$high_surrogate = 0xD800 + ($codepoint >> 10);
$low_surrogate = 0xDC00 + ($codepoint & 0x3FF);
return sprintf('\\u%04X\\u%04X', $high_surrogate, $low_surrogate);
},
$json
);
if ($escaped_json === null)
{
throw new \UnexpectedValueException('JSON contains invalid UTF-8.');
}
return $escaped_json;
}
/**
* Check whether a string contains a four-byte character unsupported by
* phpBB's MySQL utf8 schema.
*
* @param string $value
* @param bool $decode_ncr Whether to decode numeric character references.
* @return bool
*/
public static function contains_unsupported($value, $decode_ncr = true)
{
if ($decode_ncr)
{
$value = utf8_decode_ncr($value);
}
return (bool) preg_match('/[\x{10000}-\x{10FFFF}]/u', $value);
}
/**
* Remove four-byte characters unsupported by phpBB's MySQL utf8 schema.
*
* @param string $value
* @return string
*/
public static function strip_unsupported($value)
{
$value = utf8_decode_ncr($value);
return preg_replace(
'/[\x{10000}-\x{10FFFF}]/u',
' ',
$value
);
}
}