forked from eturino/Util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSON.php
68 lines (63 loc) · 1.41 KB
/
JSON.php
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
<?php
class EtuDev_Util_JSON
{
/**
* use single quote (') in the attribute
* ie: <input data-myjson='$ENCODED' />
*
* @static
*
* @param mixed $text
*
* @return null|string
*/
static public function encodetoHTMLAttribute($text)
{
try {
return static::jsonenc($text);
} catch (Exception $ex) {
return null;
}
}
static protected function jsonenc($text)
{
if (defined('JSON_UNESCAPED_UNICODE')) {
return json_encode($text, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
} else {
return json_encode($text, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
}
}
/**
* use single quote (') in the attribute
* ie: <input data-myjson='$ENCODED' />
*
* @static
*
* @param $text
*
* @return null|string
*/
static public function encodetoHTMLAttributeString($text)
{
try {
return static::jsonenc((string) $text);
} catch (Exception $ex) {
return null;
}
}
/**
* @static
*
* @param $text
*
* @return null|string
*/
static public function encode($text)
{
try {
return json_encode($text);
} catch (Exception $ex) {
return null;
}
}
}