1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\Lodash\JString; |
4
|
|
|
|
5
|
|
|
use Recca0120\Lodash\JArray; |
6
|
|
|
use Recca0120\Lodash\JString\Extensions\Chinese; |
7
|
|
|
use Recca0120\Lodash\JString\Extensions\FullCase; |
8
|
|
|
use Recca0120\Lodash\JString\Extensions\Converter; |
9
|
|
|
|
10
|
|
|
trait PHP |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Quote string with slashes in a C style. |
14
|
|
|
* |
15
|
|
|
* @param string $charlist |
16
|
|
|
* @return static |
17
|
|
|
*/ |
18
|
|
|
public function addcslashes($charlist) |
19
|
|
|
{ |
20
|
|
|
return new static(addcslashes($this->subject, $charlist)); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Quote string with slashes. |
25
|
|
|
* |
26
|
|
|
* @return static |
27
|
|
|
*/ |
28
|
|
|
public function addslashes() |
29
|
|
|
{ |
30
|
|
|
return new static(addslashes($this->subject)); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Convert binary data into hexadecimal representation. |
35
|
|
|
* |
36
|
|
|
* @return static |
37
|
|
|
*/ |
38
|
|
|
public function bin2hex() |
39
|
|
|
{ |
40
|
|
|
return new static(bin2hex($this->subject)); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
public function convertEncoding($toEncoding) |
44
|
|
|
{ |
45
|
1 |
|
return func_num_args() === 1 ? |
46
|
1 |
|
new static(mb_convert_encoding($this->subject, $toEncoding)) : |
|
|
|
|
47
|
1 |
|
new static(mb_convert_encoding($this->subject, $toEncoding, func_get_arg(0))); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
public function convertTo($variant = 'zh-tw') |
51
|
|
|
{ |
52
|
1 |
|
return new static(Converter::instance()->convertTo($this->subject, $variant)); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function chineseToNumber() |
56
|
|
|
{ |
57
|
|
|
return (new Chinese($this->subject))->toNumber(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Split a string by string. |
62
|
|
|
* |
63
|
|
|
* @param string $delimiter |
64
|
|
|
* @param int $limit |
|
|
|
|
65
|
|
|
* @return \Recca0120\Lodash\JArray |
66
|
|
|
*/ |
67
|
1 |
|
public function explode($delimiter) |
68
|
|
|
{ |
69
|
1 |
|
return func_num_args() === 1 ? |
70
|
1 |
|
new JArray(explode($delimiter, $this->subject)) : |
71
|
1 |
|
new JArray(explode($delimiter, $this->subject, func_get_arg(1))); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Convert all HTML entities to their applicable characters. |
76
|
|
|
* |
77
|
|
|
* @param int $flags |
|
|
|
|
78
|
|
|
* @return static |
79
|
|
|
*/ |
80
|
|
|
public function htmlEntityDecode() |
81
|
|
|
{ |
82
|
|
|
return func_num_args() === 0 ? |
83
|
|
|
new static(html_entity_decode($this->subject)) : |
|
|
|
|
84
|
|
|
new static(html_entity_decode($this->subject, func_get_arg(0))); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Convert all applicable characters to HTML entities. |
89
|
|
|
* |
90
|
|
|
* @param int $flags |
|
|
|
|
91
|
|
|
* @return static |
92
|
|
|
*/ |
93
|
1 |
|
public function htmlentities() |
94
|
|
|
{ |
95
|
1 |
|
return func_num_args() === 0 ? |
96
|
1 |
|
new static(htmlentities($this->subject)) : |
|
|
|
|
97
|
1 |
|
new static(htmlentities($this->subject, func_get_arg(0))); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Convert special HTML entities back to characters. |
102
|
|
|
* |
103
|
|
|
* @param int $flags |
|
|
|
|
104
|
|
|
* @return static |
105
|
|
|
*/ |
106
|
|
|
public function htmlspecialcharsDecode() |
107
|
|
|
{ |
108
|
|
|
return func_num_args() === 0 ? |
109
|
|
|
new static(htmlspecialchars_decode($this->subject)) : |
|
|
|
|
110
|
|
|
new static(htmlspecialchars_decode($this->subject, func_get_arg(0))); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Convert special characters to HTML entities. |
115
|
|
|
* |
116
|
|
|
* @param int $flags |
|
|
|
|
117
|
|
|
* @return static |
118
|
|
|
*/ |
119
|
|
|
public function htmlspecialchars() |
120
|
|
|
{ |
121
|
|
|
return func_num_args() === 0 ? |
122
|
|
|
new static(htmlspecialchars($this->subject)) : |
|
|
|
|
123
|
|
|
new static(htmlspecialchars($this->subject, func_get_arg(0))); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Make a string's first character lowercase. |
128
|
|
|
* |
129
|
|
|
* @return static |
130
|
|
|
*/ |
131
|
|
|
public function lcfirst() |
132
|
|
|
{ |
133
|
|
|
return new static(lcfirst($this->subject)); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Strip whitespace (or other characters) from the beginning of a string. |
138
|
|
|
* |
139
|
|
|
* @param string $characterMask |
140
|
|
|
* @return static |
141
|
|
|
*/ |
142
|
1 |
|
public function ltrim($characterMask = " \t\n\r\0\x0B") |
143
|
|
|
{ |
144
|
1 |
|
return new static(ltrim($this->subject, $characterMask)); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Calculate the md5 hash of a string. |
149
|
|
|
* |
150
|
|
|
* @param bool $rawOutput |
151
|
|
|
* @return static |
152
|
|
|
*/ |
153
|
|
|
public function md5($rawOutput = false) |
154
|
|
|
{ |
155
|
|
|
return new static(md5($this->subject, $rawOutput)); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Inserts HTML line breaks before all newlines in a string. |
160
|
|
|
* |
161
|
|
|
* @param bool $isXHTML |
162
|
|
|
* @return static |
163
|
|
|
*/ |
164
|
|
|
public function nl2br($isXHTML = true) |
165
|
|
|
{ |
166
|
|
|
return new static(nl2br($this->subject, $isXHTML)); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Parses the string into variables. |
171
|
|
|
* |
172
|
|
|
* @return \Recca0120\Lodash\JArray |
173
|
|
|
*/ |
174
|
|
|
public function parse() |
175
|
|
|
{ |
176
|
|
|
$results = []; |
177
|
|
|
parse_str($this->subject, $results); |
178
|
|
|
|
179
|
|
|
return new JArray($results); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Strip whitespace (or other characters) from the end of a string. |
184
|
|
|
* |
185
|
|
|
* @param string $characterMask |
186
|
|
|
* @return static |
187
|
|
|
*/ |
188
|
1 |
|
public function rtrim($characterMask = " \t\n\r\0\x0B") |
189
|
|
|
{ |
190
|
1 |
|
return new static(rtrim($this->subject, $characterMask)); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Calculate the sha1 hash of a string. |
195
|
|
|
* |
196
|
|
|
* @return static |
197
|
|
|
*/ |
198
|
|
|
public function sha1() |
199
|
|
|
{ |
200
|
|
|
return new static(sha1($this->subject)); |
|
|
|
|
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Parses input from a string according to a format. |
205
|
|
|
* |
206
|
|
|
* @param string $format |
207
|
|
|
* @return \Recca0120\Lodash\JArray |
208
|
|
|
*/ |
209
|
|
|
public function sscanf($format) |
210
|
|
|
{ |
211
|
|
|
return new JArray(sscanf($this->subject, $format)); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Pad a string to a certain length with another string. |
216
|
|
|
* |
217
|
|
|
* @param int $length |
218
|
|
|
* @param string $chars |
219
|
|
|
* @param int $type |
220
|
|
|
* @return static |
221
|
|
|
*/ |
222
|
|
|
public function pad($length = 0, $chars = ' ', $type = STR_PAD_BOTH) |
223
|
|
|
{ |
224
|
|
|
return new static(str_pad($this->subject, $length, $chars, $type)); |
|
|
|
|
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Perform the rot13 transform on a string. |
229
|
|
|
* |
230
|
|
|
* @return static |
231
|
|
|
*/ |
232
|
|
|
public function rot13() |
233
|
|
|
{ |
234
|
|
|
return new static(str_rot13($this->subject)); |
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Randomly shuffles a string. |
239
|
|
|
* |
240
|
|
|
* @return static |
241
|
|
|
*/ |
242
|
|
|
public function shuffle() |
243
|
|
|
{ |
244
|
|
|
return new static(str_shuffle($this->subject)); |
|
|
|
|
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Strip HTML and PHP tags from a string. |
249
|
|
|
* |
250
|
|
|
* @param string $allowable_tags |
|
|
|
|
251
|
|
|
* |
252
|
|
|
* @return static |
253
|
|
|
*/ |
254
|
|
|
public function stripTags() |
255
|
|
|
{ |
256
|
|
|
return func_num_args() === 0 ? |
257
|
|
|
new static(strip_tags($this->subject)) : |
|
|
|
|
258
|
|
|
new static(strip_tags($this->subject, func_get_arg(0))); |
|
|
|
|
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Un-quote string quoted with addcslashes(). |
263
|
|
|
* |
264
|
|
|
* @return static |
265
|
|
|
*/ |
266
|
|
|
public function stripcslashes() |
267
|
|
|
{ |
268
|
|
|
return new static(stripcslashes($this->subject)); |
|
|
|
|
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Un-quotes a quoted string. |
273
|
|
|
* |
274
|
|
|
* @return static |
275
|
|
|
*/ |
276
|
|
|
public function stripslashes() |
277
|
|
|
{ |
278
|
|
|
return new static(stripslashes($this->subject)); |
|
|
|
|
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Reverse a string. |
283
|
|
|
* |
284
|
|
|
* @return static |
285
|
|
|
*/ |
286
|
|
|
public function reverse() |
287
|
|
|
{ |
288
|
|
|
return new static(strrev($this->subject)); |
|
|
|
|
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
public function toFullCase() |
292
|
|
|
{ |
293
|
|
|
return new static((new FullCase($this->subject))->toFullCase()); |
|
|
|
|
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
public function toHalfCase() |
297
|
|
|
{ |
298
|
|
|
return new static((new FullCase($this->subject))->toHalfCase()); |
|
|
|
|
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Make a string's first character uppercase. |
303
|
|
|
* |
304
|
|
|
* @return static |
305
|
|
|
*/ |
306
|
|
|
public function ucfirst() |
307
|
|
|
{ |
308
|
|
|
return new static(ucfirst($this->subject)); |
|
|
|
|
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Uppercase the first character of each word in a string. |
313
|
|
|
* |
314
|
|
|
* @param string $delimiters |
315
|
|
|
* |
316
|
|
|
* @return static |
317
|
|
|
*/ |
318
|
|
|
public function ucwords($delimiters = " \t\r\n\f\v") |
319
|
|
|
{ |
320
|
|
|
return new static(ucwords($this->subject, $delimiters)); |
|
|
|
|
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Wraps a string to a given number of characters. |
325
|
|
|
* |
326
|
|
|
* @param int $width |
327
|
|
|
* @param string $break |
328
|
|
|
* @param bool $cut |
329
|
|
|
* |
330
|
|
|
* @return static |
331
|
|
|
*/ |
332
|
|
|
public function wordwrap($width = 75, $break = "\n", $cut = false) |
|
|
|
|
333
|
|
|
{ |
334
|
|
|
return new static(wordwrap($this->subject, $break, $cut)); |
|
|
|
|
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: