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