FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
phpfui/src/PHPFUI/TextHelper.php at master · phpfui/phpfui · GitHub
phpfui
/
phpfui
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
7
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
phpfui
/
src
/
PHPFUI
/
TextHelper.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
169 lines (143 loc) · 3.96 KB
Breadcrumbs
phpfui
/
src
/
PHPFUI
/
TextHelper.php
Copy path
File metadata and controls
169 lines (143 loc) · 3.96 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace
PHPFUI
;
/**
* Various useful functions for manipulating text
*/
class
TextHelper
{
/**
* Converts a PHP array to a JavaScript array that can be used
* directly as JavaScript (not JSON)
*
* @param array<string, mixed> $array of php values
* - PHP scalars (except strings) will be converted to the corresponding JavaScript type
* - strings must contain quoted strings to be represented as a string in JavaScript
* - Unquoted strings will resolve to raw JavaScript
* @param string $stringQuote optional quotes to use for string.
* Include actual quotes in your string if you
* need specify different quotes in different
* places.
* @param string $newLine Pass \n if you want human readable
* output, appends to , in output
*/
public
static
function
arrayToJS
(
array
$
array
,
string
$
stringQuote
=
''
,
string
$
newLine
=
''
) :
string
{
$
normalArray
=
\is_numeric
(
\key
(
$
array
));
$
js
=
$
normalArray
?
'
[
'
:
'
{
'
;
$
comma
=
''
;
foreach
(
$
array
as
$
key
=>
$
value
)
{
$
js
.=
$
comma
;
if
(!
$
normalArray
)
{
// use object notation
if
(!
\ctype_alpha
(
$
key
[
0
]))
{
$
key
=
"{
$
stringQuote
}{
$
key
}{
$
stringQuote
}"
;
}
$
js
.=
$
key
.
'
:
'
;
}
$
comma
=
'
,
'
.
$
newLine
;
switch
(
\gettype
(
$
value
))
{
/** @noinspection PhpMissingBreakStatementInspection */
case
'
object
'
:
$
value
=
\json_decode
(
\json_encode
(
$
value
),
true
,
512
);
// Intentionally fall through
case
'
array
'
:
$
js
.=
self
::
arrayToJS
(
$
value
,
$
stringQuote
);
break
;
case
'
boolean
'
:
$
js
.=
$
value
?
'
true
'
:
'
false
'
;
break
;
/** @noinspection PhpMissingBreakStatementInspection */
case
'
string
'
:
$
value
=
"{
$
stringQuote
}{
$
value
}{
$
stringQuote
}"
;
// Intentionally fall through
case
'
integer
'
:
case
'
double
'
:
$
js
.=
$
value
;
break
;
case
'
resource
'
:
case
'
NULL
'
:
case
'
unknown type
'
:
$
js
.=
'
null
'
;
}
}
return
$
js
. (
$
normalArray
?
'
]
'
:
'
}
'
);
}
/**
* Split a string into words based on capital letters. Successive capital letters are considered an abbreviation and grouped together.
*/
public
static
function
capitalSplit
(
string
$
key
) :
string
{
$
len
=
\strlen
(
$
key
);
$
space
=
$
output
=
''
;
$
lastCapitalized
=
false
;
$
consecutiveCaps
=
0
;
for
(
$
i
=
0
;
$
i
<
$
len
; ++
$
i
)
{
$
char
=
$
key
[
$
i
];
if
(
0
==
$
i
)
{
$
char
=
\strtoupper
(
$
char
);
}
if
(
\ctype_upper
(
$
char
))
{
if
(!
$
lastCapitalized
)
{
$
output
.=
$
space
;
}
++
$
consecutiveCaps
;
$
space
=
'
'
;
$
lastCapitalized
=
true
;
}
elseif
(
$
lastCapitalized
)
{
$
length
=
\strlen
(
$
output
);
if
(
$
length
>
1
&&
$
consecutiveCaps
>
1
)
{
$
output
=
\substr
(
$
output
,
0
,
$
length
-
1
) .
'
'
.
$
output
[
$
length
-
1
];
}
$
consecutiveCaps
=
0
;
$
lastCapitalized
=
false
;
}
$
output
.=
$
char
;
}
return
$
output
;
}
/**
* Shorthand to encode a string in UTF-8
*
* @param ?string $string to encode
*/
public
static
function
htmlentities
(?
string
$
string
) :
string
{
if
(!
$
string
)
{
return
''
;
}
return
\htmlentities
(
$
string
,
ENT_QUOTES
|
ENT_SUBSTITUTE
,
'
UTF-8
'
,
false
);
}
/** @param array<string> $match */
public
static
function
replace_unicode_escape_sequence
(
array
$
match
) :
string
{
return
\mb_convert_encoding
(
\pack
(
'
H*
'
,
$
match
[
1
]),
'
UTF-8
'
,
'
UCS-2BE
'
);
}
/**
* Decode hmtl entities
*
* @param ?string $string to decode
*/
public
static
function
unhtmlentities
(?
string
$
string
) :
string
{
if
(!
$
string
)
{
return
''
;
}
return
\htmlspecialchars_decode
(
$
string
,
ENT_QUOTES
|
ENT_HTML5
);
}
public
static
function
unicode_decode
(
string
$
str
) :
string
{
return
\preg_replace_callback
(
'
/
\\\\
u([0-9a-f]{4})/i
'
,
'
\PHPFUI\TextHelper::replace_unicode_escape_sequence
'
,
$
str
);
}
}
Back
|
FazBrowse Home
|
New Git URL