1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stringy; |
4
|
|
|
|
5
|
|
|
use voku\helper\AntiXSS; |
6
|
|
|
use voku\helper\EmailCheck; |
7
|
|
|
use voku\helper\URLify; |
8
|
|
|
use voku\helper\UTF8; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Stringy |
12
|
|
|
* |
13
|
|
|
* @package Stringy |
14
|
|
|
*/ |
15
|
|
|
class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* An instance's string. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $str; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The string's encoding, which should be one of the mbstring module's |
26
|
|
|
* supported encodings. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $encoding; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Initializes a Stringy object and assigns both str and encoding properties |
34
|
|
|
* the supplied values. $str is cast to a string prior to assignment, and if |
35
|
|
|
* $encoding is not specified, it defaults to mb_internal_encoding(). Throws |
36
|
|
|
* an InvalidArgumentException if the first argument is an array or object |
37
|
|
|
* without a __toString method. |
38
|
|
|
* |
39
|
|
|
* @param mixed $str Value to modify, after being cast to string |
40
|
|
|
* @param string $encoding The character encoding |
41
|
|
|
* |
42
|
|
|
* @throws \InvalidArgumentException if an array or object without a |
43
|
|
|
* __toString method is passed as the first argument |
44
|
|
|
*/ |
45
|
1073 |
|
public function __construct($str = '', $encoding = null) |
46
|
|
|
{ |
47
|
1073 |
|
if (is_array($str)) { |
48
|
1 |
|
throw new \InvalidArgumentException( |
49
|
1 |
|
'Passed value cannot be an array' |
50
|
|
|
); |
51
|
1072 |
|
} elseif (is_object($str) && !method_exists($str, '__toString')) { |
52
|
1 |
|
throw new \InvalidArgumentException( |
53
|
1 |
|
'Passed object must have a __toString method' |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// don't throw a notice on PHP 5.3 |
58
|
1071 |
|
if (!defined('ENT_SUBSTITUTE')) { |
59
|
|
|
define('ENT_SUBSTITUTE', 8); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// init |
63
|
1071 |
|
UTF8::checkForSupport(); |
64
|
|
|
|
65
|
1071 |
|
$this->str = (string)$str; |
66
|
|
|
|
67
|
1071 |
|
if ($encoding) { |
|
|
|
|
68
|
850 |
|
$this->encoding = $encoding; |
69
|
|
|
} else { |
70
|
699 |
|
UTF8::mbstring_loaded(); |
71
|
699 |
|
$this->encoding = mb_internal_encoding(); |
72
|
|
|
} |
73
|
|
|
|
74
|
1071 |
|
if ($encoding) { |
|
|
|
|
75
|
850 |
|
$this->encoding = $encoding; |
76
|
|
|
} else { |
77
|
699 |
|
$this->encoding = mb_internal_encoding(); |
78
|
|
|
} |
79
|
1071 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns the value in $str. |
83
|
|
|
* |
84
|
|
|
* @return string The current value of the $str property |
85
|
|
|
*/ |
86
|
150 |
|
public function __toString() |
87
|
|
|
{ |
88
|
150 |
|
return (string)$this->str; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Returns a new string with $string appended. |
93
|
|
|
* |
94
|
|
|
* @param string $string The string to append |
95
|
|
|
* |
96
|
|
|
* @return Stringy Object with appended $string |
97
|
|
|
*/ |
98
|
5 |
|
public function append($string) |
99
|
|
|
{ |
100
|
5 |
|
return static::create($this->str . $string, $this->encoding); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Append an password (limited to chars that are good readable). |
105
|
|
|
* |
106
|
|
|
* @param int $length length of the random string |
107
|
|
|
* |
108
|
|
|
* @return Stringy Object with appended password |
109
|
|
|
*/ |
110
|
1 |
|
public function appendPassword($length) |
111
|
|
|
{ |
112
|
1 |
|
$possibleChars = '2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ'; |
113
|
|
|
|
114
|
1 |
|
return $this->appendRandomString($length, $possibleChars); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Append an unique identifier. |
119
|
|
|
* |
120
|
|
|
* @param string|int $extraPrefix |
121
|
|
|
* |
122
|
|
|
* @return Stringy Object with appended unique identifier as md5-hash |
123
|
|
|
*/ |
124
|
1 |
|
public function appendUniqueIdentifier($extraPrefix = '') |
|
|
|
|
125
|
|
|
{ |
126
|
1 |
|
$prefix = mt_rand() . |
127
|
1 |
|
session_id() . |
128
|
1 |
|
(isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '') . |
129
|
1 |
|
(isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '') . |
130
|
1 |
|
$extraPrefix; |
131
|
|
|
|
132
|
1 |
|
return $this->append(md5(uniqid($prefix, true) . $prefix)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Append an random string. |
137
|
|
|
* |
138
|
|
|
* @param int $length length of the random string |
139
|
|
|
* @param string $possibleChars characters string for the random selection |
140
|
|
|
* |
141
|
|
|
* @return Stringy Object with appended random string |
142
|
|
|
*/ |
143
|
2 |
|
public function appendRandomString($length, $possibleChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') |
144
|
|
|
{ |
145
|
|
|
// init |
146
|
2 |
|
$i = 0; |
147
|
2 |
|
$length = (int)$length; |
148
|
2 |
|
$str = $this->str; |
149
|
2 |
|
$maxlength = UTF8::strlen($possibleChars, $this->encoding); |
150
|
|
|
|
151
|
2 |
|
if ($maxlength === 0) { |
152
|
1 |
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// add random chars |
156
|
2 |
|
while ($i < $length) { |
157
|
2 |
|
$char = UTF8::substr($possibleChars, mt_rand(0, $maxlength - 1), 1, $this->encoding); |
158
|
2 |
|
$str .= $char; |
159
|
2 |
|
$i++; |
160
|
|
|
} |
161
|
|
|
|
162
|
2 |
|
return $this->append($str); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Creates a Stringy object and assigns both str and encoding properties |
167
|
|
|
* the supplied values. $str is cast to a string prior to assignment, and if |
168
|
|
|
* $encoding is not specified, it defaults to mb_internal_encoding(). It |
169
|
|
|
* then returns the initialized object. Throws an InvalidArgumentException |
170
|
|
|
* if the first argument is an array or object without a __toString method. |
171
|
|
|
* |
172
|
|
|
* @param mixed $str Value to modify, after being cast to string |
173
|
|
|
* @param string $encoding The character encoding |
174
|
|
|
* |
175
|
|
|
* @return Stringy A Stringy object |
176
|
|
|
* @throws \InvalidArgumentException if an array or object without a |
177
|
|
|
* __toString method is passed as the first argument |
178
|
|
|
*/ |
179
|
1063 |
|
public static function create($str = '', $encoding = null) |
180
|
|
|
{ |
181
|
1063 |
|
return new static($str, $encoding); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Returns the substring between $start and $end, if found, or an empty |
186
|
|
|
* string. An optional offset may be supplied from which to begin the |
187
|
|
|
* search for the start string. |
188
|
|
|
* |
189
|
|
|
* @param string $start Delimiter marking the start of the substring |
190
|
|
|
* @param string $end Delimiter marking the end of the substring |
191
|
|
|
* @param int $offset Index from which to begin the search |
192
|
|
|
* |
193
|
|
|
* @return Stringy Object whose $str is a substring between $start and $end |
194
|
|
|
*/ |
195
|
16 |
|
public function between($start, $end, $offset = 0) |
196
|
|
|
{ |
197
|
16 |
|
$startIndex = $this->indexOf($start, $offset); |
198
|
16 |
|
if ($startIndex === false) { |
199
|
2 |
|
return static::create('', $this->encoding); |
200
|
|
|
} |
201
|
|
|
|
202
|
14 |
|
$substrIndex = $startIndex + UTF8::strlen($start, $this->encoding); |
203
|
14 |
|
$endIndex = $this->indexOf($end, $substrIndex); |
204
|
14 |
|
if ($endIndex === false) { |
205
|
2 |
|
return static::create('', $this->encoding); |
206
|
|
|
} |
207
|
|
|
|
208
|
12 |
|
return $this->substr($substrIndex, $endIndex - $substrIndex); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Returns the index of the first occurrence of $needle in the string, |
213
|
|
|
* and false if not found. Accepts an optional offset from which to begin |
214
|
|
|
* the search. |
215
|
|
|
* |
216
|
|
|
* @param string $needle Substring to look for |
217
|
|
|
* @param int $offset Offset from which to search |
218
|
|
|
* |
219
|
|
|
* @return int|bool The occurrence's index if found, otherwise false |
220
|
|
|
*/ |
221
|
28 |
|
public function indexOf($needle, $offset = 0) |
222
|
|
|
{ |
223
|
28 |
|
return UTF8::strpos($this->str, (string)$needle, (int)$offset, $this->encoding); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Returns the substring beginning at $start with the specified $length. |
228
|
|
|
* It differs from the UTF8::substr() function in that providing a $length of |
229
|
|
|
* null will return the rest of the string, rather than an empty string. |
230
|
|
|
* |
231
|
|
|
* @param int $start Position of the first character to use |
232
|
|
|
* @param int $length Maximum number of characters used |
233
|
|
|
* |
234
|
|
|
* @return Stringy Object with its $str being the substring |
235
|
|
|
*/ |
236
|
64 |
|
public function substr($start, $length = null) |
237
|
|
|
{ |
238
|
64 |
|
if ($length === null) { |
239
|
19 |
|
$length = $this->length(); |
240
|
|
|
} |
241
|
|
|
|
242
|
64 |
|
$str = UTF8::substr($this->str, $start, $length, $this->encoding); |
243
|
|
|
|
244
|
64 |
|
return static::create($str, $this->encoding); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Returns the length of the string. |
249
|
|
|
* |
250
|
|
|
* @return int The number of characters in $str given the encoding |
251
|
|
|
*/ |
252
|
248 |
|
public function length() |
253
|
|
|
{ |
254
|
248 |
|
return UTF8::strlen($this->str, $this->encoding); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Trims the string and replaces consecutive whitespace characters with a |
259
|
|
|
* single space. This includes tabs and newline characters, as well as |
260
|
|
|
* multibyte whitespace such as the thin space and ideographic space. |
261
|
|
|
* |
262
|
|
|
* @return Stringy Object with a trimmed $str and condensed whitespace |
263
|
|
|
*/ |
264
|
52 |
|
public function collapseWhitespace() |
265
|
|
|
{ |
266
|
52 |
|
return $this->regexReplace('[[:space:]]+', ' ')->trim(); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Returns a string with whitespace removed from the start and end of the |
271
|
|
|
* string. Supports the removal of unicode whitespace. Accepts an optional |
272
|
|
|
* string of characters to strip instead of the defaults. |
273
|
|
|
* |
274
|
|
|
* @param string $chars Optional string of characters to strip |
275
|
|
|
* |
276
|
|
|
* @return Stringy Object with a trimmed $str |
277
|
|
|
*/ |
278
|
153 |
View Code Duplication |
public function trim($chars = null) |
|
|
|
|
279
|
|
|
{ |
280
|
153 |
|
if (!$chars) { |
|
|
|
|
281
|
152 |
|
$chars = '[:space:]'; |
282
|
|
|
} else { |
283
|
1 |
|
$chars = preg_quote($chars, '/'); |
284
|
|
|
} |
285
|
|
|
|
286
|
153 |
|
return $this->regexReplace("^[$chars]+|[$chars]+\$", ''); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Replaces all occurrences of $pattern in $str by $replacement. |
291
|
|
|
* |
292
|
|
|
* @param string $pattern The regular expression pattern |
293
|
|
|
* @param string $replacement The string to replace with |
294
|
|
|
* @param string $options Matching conditions to be used |
295
|
|
|
* |
296
|
|
|
* @return Stringy Object with the result2ing $str after the replacements |
297
|
|
|
*/ |
298
|
223 |
|
public function regexReplace($pattern, $replacement, $options = '') |
299
|
|
|
{ |
300
|
223 |
|
if ($options === 'msr') { |
301
|
8 |
|
$options = 'ms'; |
302
|
|
|
} |
303
|
|
|
|
304
|
223 |
|
$str = preg_replace( |
305
|
223 |
|
'/' . $pattern . '/u' . $options, |
306
|
|
|
$replacement, |
307
|
223 |
|
$this->str |
308
|
|
|
); |
309
|
|
|
|
310
|
223 |
|
return static::create($str, $this->encoding); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Returns true if the string contains all $needles, false otherwise. By |
315
|
|
|
* default the comparison is case-sensitive, but can be made insensitive by |
316
|
|
|
* setting $caseSensitive to false. |
317
|
|
|
* |
318
|
|
|
* @param array $needles SubStrings to look for |
319
|
|
|
* @param bool $caseSensitive Whether or not to enforce case-sensitivity |
320
|
|
|
* |
321
|
|
|
* @return bool Whether or not $str contains $needle |
322
|
|
|
*/ |
323
|
43 |
View Code Duplication |
public function containsAll($needles, $caseSensitive = true) |
|
|
|
|
324
|
|
|
{ |
325
|
|
|
/** @noinspection IsEmptyFunctionUsageInspection */ |
326
|
43 |
|
if (empty($needles)) { |
327
|
1 |
|
return false; |
328
|
|
|
} |
329
|
|
|
|
330
|
42 |
|
foreach ($needles as $needle) { |
331
|
42 |
|
if (!$this->contains($needle, $caseSensitive)) { |
332
|
42 |
|
return false; |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
336
|
24 |
|
return true; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Returns true if the string contains $needle, false otherwise. By default |
341
|
|
|
* the comparison is case-sensitive, but can be made insensitive by setting |
342
|
|
|
* $caseSensitive to false. |
343
|
|
|
* |
344
|
|
|
* @param string $needle Substring to look for |
345
|
|
|
* @param bool $caseSensitive Whether or not to enforce case-sensitivity |
346
|
|
|
* |
347
|
|
|
* @return bool Whether or not $str contains $needle |
348
|
|
|
*/ |
349
|
105 |
|
public function contains($needle, $caseSensitive = true) |
350
|
|
|
{ |
351
|
105 |
|
$encoding = $this->encoding; |
352
|
|
|
|
353
|
105 |
|
if ($caseSensitive) { |
354
|
55 |
|
return (UTF8::strpos($this->str, $needle, 0, $encoding) !== false); |
355
|
|
|
} else { |
356
|
50 |
|
return (UTF8::stripos($this->str, $needle, 0, $encoding) !== false); |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Returns true if the string contains any $needles, false otherwise. By |
362
|
|
|
* default the comparison is case-sensitive, but can be made insensitive by |
363
|
|
|
* setting $caseSensitive to false. |
364
|
|
|
* |
365
|
|
|
* @param array $needles SubStrings to look for |
366
|
|
|
* @param bool $caseSensitive Whether or not to enforce case-sensitivity |
367
|
|
|
* |
368
|
|
|
* @return bool Whether or not $str contains $needle |
369
|
|
|
*/ |
370
|
43 |
View Code Duplication |
public function containsAny($needles, $caseSensitive = true) |
|
|
|
|
371
|
|
|
{ |
372
|
|
|
/** @noinspection IsEmptyFunctionUsageInspection */ |
373
|
43 |
|
if (empty($needles)) { |
374
|
1 |
|
return false; |
375
|
|
|
} |
376
|
|
|
|
377
|
42 |
|
foreach ($needles as $needle) { |
378
|
42 |
|
if ($this->contains($needle, $caseSensitive)) { |
379
|
42 |
|
return true; |
380
|
|
|
} |
381
|
|
|
} |
382
|
|
|
|
383
|
18 |
|
return false; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Returns the length of the string, implementing the countable interface. |
388
|
|
|
* |
389
|
|
|
* @return int The number of characters in the string, given the encoding |
390
|
|
|
*/ |
391
|
1 |
|
public function count() |
392
|
|
|
{ |
393
|
1 |
|
return $this->length(); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Returns the number of occurrences of $substring in the given string. |
398
|
|
|
* By default, the comparison is case-sensitive, but can be made insensitive |
399
|
|
|
* by setting $caseSensitive to false. |
400
|
|
|
* |
401
|
|
|
* @param string $substring The substring to search for |
402
|
|
|
* @param bool $caseSensitive Whether or not to enforce case-sensitivity |
403
|
|
|
* |
404
|
|
|
* @return int The number of $substring occurrences |
405
|
|
|
*/ |
406
|
15 |
|
public function countSubstr($substring, $caseSensitive = true) |
407
|
|
|
{ |
408
|
15 |
|
if ($caseSensitive) { |
409
|
9 |
|
return UTF8::substr_count($this->str, $substring, 0, null, $this->encoding); |
410
|
|
|
} |
411
|
|
|
|
412
|
6 |
|
$str = UTF8::strtoupper($this->str, $this->encoding); |
413
|
6 |
|
$substring = UTF8::strtoupper($substring, $this->encoding); |
414
|
|
|
|
415
|
6 |
|
return UTF8::substr_count($str, $substring, 0, null, $this->encoding); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Returns a lowercase and trimmed string separated by dashes. Dashes are |
420
|
|
|
* inserted before uppercase characters (with the exception of the first |
421
|
|
|
* character of the string), and in place of spaces as well as underscores. |
422
|
|
|
* |
423
|
|
|
* @return Stringy Object with a dasherized $str |
424
|
|
|
*/ |
425
|
19 |
|
public function dasherize() |
426
|
|
|
{ |
427
|
19 |
|
return $this->delimit('-'); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Returns a lowercase and trimmed string separated by the given delimiter. |
432
|
|
|
* Delimiters are inserted before uppercase characters (with the exception |
433
|
|
|
* of the first character of the string), and in place of spaces, dashes, |
434
|
|
|
* and underscores. Alpha delimiters are not converted to lowercase. |
435
|
|
|
* |
436
|
|
|
* @param string $delimiter Sequence used to separate parts of the string |
437
|
|
|
* |
438
|
|
|
* @return Stringy Object with a delimited $str |
439
|
|
|
*/ |
440
|
49 |
|
public function delimit($delimiter) |
441
|
|
|
{ |
442
|
49 |
|
$str = $this->trim(); |
443
|
|
|
|
444
|
49 |
|
$str = preg_replace('/\B([A-Z])/u', '-\1', $str); |
445
|
|
|
|
446
|
49 |
|
$str = UTF8::strtolower($str, $this->encoding); |
|
|
|
|
447
|
|
|
|
448
|
49 |
|
$str = preg_replace('/[-_\s]+/u', $delimiter, $str); |
449
|
|
|
|
450
|
49 |
|
return static::create($str, $this->encoding); |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Ensures that the string begins with $substring. If it doesn't, it's |
455
|
|
|
* prepended. |
456
|
|
|
* |
457
|
|
|
* @param string $substring The substring to add if not present |
458
|
|
|
* |
459
|
|
|
* @return Stringy Object with its $str prefixed by the $substring |
460
|
|
|
*/ |
461
|
10 |
View Code Duplication |
public function ensureLeft($substring) |
|
|
|
|
462
|
|
|
{ |
463
|
10 |
|
$stringy = static::create($this->str, $this->encoding); |
464
|
|
|
|
465
|
10 |
|
if (!$stringy->startsWith($substring)) { |
466
|
4 |
|
$stringy->str = $substring . $stringy->str; |
467
|
|
|
} |
468
|
|
|
|
469
|
10 |
|
return $stringy; |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* Returns true if the string begins with $substring, false otherwise. By |
474
|
|
|
* default, the comparison is case-sensitive, but can be made insensitive |
475
|
|
|
* by setting $caseSensitive to false. |
476
|
|
|
* |
477
|
|
|
* @param string $substring The substring to look for |
478
|
|
|
* @param bool $caseSensitive Whether or not to enforce case-sensitivity |
479
|
|
|
* |
480
|
|
|
* @return bool Whether or not $str starts with $substring |
481
|
|
|
*/ |
482
|
45 |
|
public function startsWith($substring, $caseSensitive = true) |
483
|
|
|
{ |
484
|
45 |
|
$str = $this->str; |
485
|
|
|
|
486
|
45 |
View Code Duplication |
if (!$caseSensitive) { |
|
|
|
|
487
|
8 |
|
$substring = UTF8::strtolower($substring, $this->encoding); |
488
|
8 |
|
$str = UTF8::strtolower($this->str, $this->encoding); |
489
|
|
|
} |
490
|
|
|
|
491
|
45 |
|
return UTF8::strpos($str, $substring, $this->encoding) === 0; |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* Returns true if the string begins with any $substrings, false otherwise. By |
496
|
|
|
* default the comparison is case-sensitive, but can be made insensitive by |
497
|
|
|
* setting $caseSensitive to false. |
498
|
|
|
* |
499
|
|
|
* @param array $substrings Substrings to look for |
500
|
|
|
* @param bool $caseSensitive Whether or not to enforce case-sensitivity |
501
|
|
|
* |
502
|
|
|
* @return bool Whether or not $str starts with $substring |
503
|
|
|
*/ |
504
|
12 |
|
public function startsWithAny(array $substrings, $caseSensitive = true) |
505
|
|
|
{ |
506
|
12 |
|
if (empty($substrings)) { |
507
|
|
|
return false; |
508
|
|
|
} |
509
|
|
|
|
510
|
12 |
|
foreach ($substrings as $substring) { |
511
|
12 |
|
if ($this->startsWith($substring, $caseSensitive)) { |
512
|
12 |
|
return true; |
513
|
|
|
} |
514
|
|
|
} |
515
|
|
|
|
516
|
6 |
|
return false; |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* Ensures that the string ends with $substring. If it doesn't, it's |
521
|
|
|
* appended. |
522
|
|
|
* |
523
|
|
|
* @param string $substring The substring to add if not present |
524
|
|
|
* |
525
|
|
|
* @return Stringy Object with its $str suffixed by the $substring |
526
|
|
|
*/ |
527
|
10 |
View Code Duplication |
public function ensureRight($substring) |
|
|
|
|
528
|
|
|
{ |
529
|
10 |
|
$stringy = static::create($this->str, $this->encoding); |
530
|
|
|
|
531
|
10 |
|
if (!$stringy->endsWith($substring)) { |
532
|
4 |
|
$stringy->str .= $substring; |
533
|
|
|
} |
534
|
|
|
|
535
|
10 |
|
return $stringy; |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
/** |
539
|
|
|
* Returns true if the string ends with $substring, false otherwise. By |
540
|
|
|
* default, the comparison is case-sensitive, but can be made insensitive |
541
|
|
|
* by setting $caseSensitive to false. |
542
|
|
|
* |
543
|
|
|
* @param string $substring The substring to look for |
544
|
|
|
* @param bool $caseSensitive Whether or not to enforce case-sensitivity |
545
|
|
|
* |
546
|
|
|
* @return bool Whether or not $str ends with $substring |
547
|
|
|
*/ |
548
|
33 |
|
public function endsWith($substring, $caseSensitive = true) |
549
|
|
|
{ |
550
|
33 |
|
$substringLength = UTF8::strlen($substring, $this->encoding); |
551
|
33 |
|
$strLength = $this->length(); |
552
|
|
|
|
553
|
33 |
|
$endOfStr = UTF8::substr( |
554
|
33 |
|
$this->str, |
555
|
33 |
|
$strLength - $substringLength, |
556
|
|
|
$substringLength, |
557
|
33 |
|
$this->encoding |
558
|
|
|
); |
559
|
|
|
|
560
|
33 |
View Code Duplication |
if (!$caseSensitive) { |
|
|
|
|
561
|
4 |
|
$substring = UTF8::strtolower($substring, $this->encoding); |
562
|
4 |
|
$endOfStr = UTF8::strtolower($endOfStr, $this->encoding); |
|
|
|
|
563
|
|
|
} |
564
|
|
|
|
565
|
33 |
|
return (string)$substring === $endOfStr; |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* Returns the first $n characters of the string. |
570
|
|
|
* |
571
|
|
|
* @param int $n Number of characters to retrieve from the start |
572
|
|
|
* |
573
|
|
|
* @return Stringy Object with its $str being the first $n chars |
574
|
|
|
*/ |
575
|
12 |
View Code Duplication |
public function first($n) |
|
|
|
|
576
|
|
|
{ |
577
|
12 |
|
$stringy = static::create($this->str, $this->encoding); |
578
|
|
|
|
579
|
12 |
|
if ($n < 0) { |
580
|
2 |
|
$stringy->str = ''; |
581
|
|
|
} else { |
582
|
10 |
|
return $stringy->substr(0, $n); |
583
|
|
|
} |
584
|
|
|
|
585
|
2 |
|
return $stringy; |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
/** |
589
|
|
|
* Returns the encoding used by the Stringy object. |
590
|
|
|
* |
591
|
|
|
* @return string The current value of the $encoding property |
592
|
|
|
*/ |
593
|
3 |
|
public function getEncoding() |
594
|
|
|
{ |
595
|
3 |
|
return $this->encoding; |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
/** |
599
|
|
|
* Returns a new ArrayIterator, thus implementing the IteratorAggregate |
600
|
|
|
* interface. The ArrayIterator's constructor is passed an array of chars |
601
|
|
|
* in the multibyte string. This enables the use of foreach with instances |
602
|
|
|
* of Stringy\Stringy. |
603
|
|
|
* |
604
|
|
|
* @return \ArrayIterator An iterator for the characters in the string |
605
|
|
|
*/ |
606
|
1 |
|
public function getIterator() |
607
|
|
|
{ |
608
|
1 |
|
return new \ArrayIterator($this->chars()); |
609
|
|
|
} |
610
|
|
|
|
611
|
|
|
/** |
612
|
|
|
* Returns an array consisting of the characters in the string. |
613
|
|
|
* |
614
|
|
|
* @return array An array of string chars |
615
|
|
|
*/ |
616
|
4 |
|
public function chars() |
617
|
|
|
{ |
618
|
|
|
// init |
619
|
4 |
|
$chars = array(); |
620
|
4 |
|
$l = $this->length(); |
621
|
|
|
|
622
|
4 |
|
for ($i = 0; $i < $l; $i++) { |
623
|
3 |
|
$chars[] = $this->at($i)->str; |
624
|
|
|
} |
625
|
|
|
|
626
|
4 |
|
return $chars; |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
/** |
630
|
|
|
* Returns the character at $index, with indexes starting at 0. |
631
|
|
|
* |
632
|
|
|
* @param int $index Position of the character |
633
|
|
|
* |
634
|
|
|
* @return Stringy The character at $index |
635
|
|
|
*/ |
636
|
11 |
|
public function at($index) |
637
|
|
|
{ |
638
|
11 |
|
return $this->substr($index, 1); |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
/** |
642
|
|
|
* Returns true if the string contains a lower case char, false |
643
|
|
|
* otherwise. |
644
|
|
|
* |
645
|
|
|
* @return bool Whether or not the string contains a lower case character. |
646
|
|
|
*/ |
647
|
12 |
|
public function hasLowerCase() |
648
|
|
|
{ |
649
|
12 |
|
return $this->matchesPattern('.*[[:lower:]]'); |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
/** |
653
|
|
|
* Returns true if $str matches the supplied pattern, false otherwise. |
654
|
|
|
* |
655
|
|
|
* @param string $pattern Regex pattern to match against |
656
|
|
|
* |
657
|
|
|
* @return bool Whether or not $str matches the pattern |
658
|
|
|
*/ |
659
|
91 |
|
private function matchesPattern($pattern) |
660
|
|
|
{ |
661
|
91 |
|
if (preg_match('/' . $pattern . '/u', $this->str)) { |
662
|
54 |
|
return true; |
663
|
|
|
} else { |
664
|
37 |
|
return false; |
665
|
|
|
} |
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
/** |
669
|
|
|
* Returns true if the string contains an upper case char, false |
670
|
|
|
* otherwise. |
671
|
|
|
* |
672
|
|
|
* @return bool Whether or not the string contains an upper case character. |
673
|
|
|
*/ |
674
|
12 |
|
public function hasUpperCase() |
675
|
|
|
{ |
676
|
12 |
|
return $this->matchesPattern('.*[[:upper:]]'); |
677
|
|
|
} |
678
|
|
|
|
679
|
|
|
/** |
680
|
|
|
* Convert all HTML entities to their applicable characters. |
681
|
|
|
* |
682
|
|
|
* @param int|null $flags Optional flags |
683
|
|
|
* |
684
|
|
|
* @return Stringy Object with the resulting $str after being html decoded. |
685
|
|
|
*/ |
686
|
5 |
|
public function htmlDecode($flags = ENT_COMPAT) |
687
|
|
|
{ |
688
|
5 |
|
$str = UTF8::html_entity_decode($this->str, $flags, $this->encoding); |
689
|
|
|
|
690
|
5 |
|
return static::create($str, $this->encoding); |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
/** |
694
|
|
|
* Convert all applicable characters to HTML entities. |
695
|
|
|
* |
696
|
|
|
* @param int|null $flags Optional flags |
697
|
|
|
* |
698
|
|
|
* @return Stringy Object with the resulting $str after being html encoded. |
699
|
|
|
*/ |
700
|
5 |
|
public function htmlEncode($flags = ENT_COMPAT) |
701
|
|
|
{ |
702
|
5 |
|
$str = UTF8::htmlentities($this->str, $flags, $this->encoding); |
703
|
|
|
|
704
|
5 |
|
return static::create($str, $this->encoding); |
705
|
|
|
} |
706
|
|
|
|
707
|
|
|
/** |
708
|
|
|
* Capitalizes the first word of the string, replaces underscores with |
709
|
|
|
* spaces, and strips '_id'. |
710
|
|
|
* |
711
|
|
|
* @return Stringy Object with a humanized $str |
712
|
|
|
*/ |
713
|
3 |
|
public function humanize() |
714
|
|
|
{ |
715
|
3 |
|
$str = UTF8::str_replace(array('_id', '_'), array('', ' '), $this->str); |
716
|
|
|
|
717
|
3 |
|
return static::create($str, $this->encoding)->trim()->upperCaseFirst(); |
718
|
|
|
} |
719
|
|
|
|
720
|
|
|
/** |
721
|
|
|
* Converts the first character of the supplied string to upper case. |
722
|
|
|
* |
723
|
|
|
* @return Stringy Object with the first character of $str being upper case |
724
|
|
|
*/ |
725
|
27 |
View Code Duplication |
public function upperCaseFirst() |
|
|
|
|
726
|
|
|
{ |
727
|
27 |
|
$first = UTF8::substr($this->str, 0, 1, $this->encoding); |
728
|
27 |
|
$rest = UTF8::substr( |
729
|
27 |
|
$this->str, |
730
|
27 |
|
1, |
731
|
27 |
|
$this->length() - 1, |
732
|
27 |
|
$this->encoding |
733
|
|
|
); |
734
|
|
|
|
735
|
27 |
|
$str = UTF8::strtoupper($first, $this->encoding) . $rest; |
|
|
|
|
736
|
|
|
|
737
|
27 |
|
return static::create($str, $this->encoding); |
738
|
|
|
} |
739
|
|
|
|
740
|
|
|
/** |
741
|
|
|
* Returns the index of the last occurrence of $needle in the string, |
742
|
|
|
* and false if not found. Accepts an optional offset from which to begin |
743
|
|
|
* the search. Offsets may be negative to count from the last character |
744
|
|
|
* in the string. |
745
|
|
|
* |
746
|
|
|
* @param string $needle Substring to look for |
747
|
|
|
* @param int $offset Offset from which to search |
748
|
|
|
* |
749
|
|
|
* @return int|bool The last occurrence's index if found, otherwise false |
750
|
|
|
*/ |
751
|
12 |
|
public function indexOfLast($needle, $offset = 0) |
752
|
|
|
{ |
753
|
12 |
|
return UTF8::strrpos($this->str, (string)$needle, (int)$offset, $this->encoding); |
754
|
|
|
} |
755
|
|
|
|
756
|
|
|
/** |
757
|
|
|
* Inserts $substring into the string at the $index provided. |
758
|
|
|
* |
759
|
|
|
* @param string $substring String to be inserted |
760
|
|
|
* @param int $index The index at which to insert the substring |
761
|
|
|
* |
762
|
|
|
* @return Stringy Object with the resulting $str after the insertion |
763
|
|
|
*/ |
764
|
8 |
View Code Duplication |
public function insert($substring, $index) |
|
|
|
|
765
|
|
|
{ |
766
|
8 |
|
$stringy = static::create($this->str, $this->encoding); |
767
|
8 |
|
if ($index > $stringy->length()) { |
768
|
1 |
|
return $stringy; |
769
|
|
|
} |
770
|
|
|
|
771
|
7 |
|
$start = UTF8::substr($stringy->str, 0, $index, $stringy->encoding); |
772
|
7 |
|
$end = UTF8::substr($stringy->str, $index, $stringy->length(), $stringy->encoding); |
773
|
|
|
|
774
|
7 |
|
$stringy->str = $start . $substring . $end; |
775
|
|
|
|
776
|
7 |
|
return $stringy; |
777
|
|
|
} |
778
|
|
|
|
779
|
|
|
/** |
780
|
|
|
* Returns true if the string contains only alphabetic chars, false |
781
|
|
|
* otherwise. |
782
|
|
|
* |
783
|
|
|
* @return bool Whether or not $str contains only alphabetic chars |
784
|
|
|
*/ |
785
|
10 |
|
public function isAlpha() |
786
|
|
|
{ |
787
|
10 |
|
return $this->matchesPattern('^[[:alpha:]]*$'); |
788
|
|
|
} |
789
|
|
|
|
790
|
|
|
/** |
791
|
|
|
* Determine whether the string is considered to be empty. |
792
|
|
|
* |
793
|
|
|
* A variable is considered empty if it does not exist or if its value equals FALSE. |
794
|
|
|
* empty() does not generate a warning if the variable does not exist. |
795
|
|
|
* |
796
|
|
|
* @return bool |
797
|
|
|
*/ |
798
|
|
|
public function isEmpty() |
799
|
|
|
{ |
800
|
|
|
return empty($this->str); |
801
|
|
|
} |
802
|
|
|
|
803
|
|
|
/** |
804
|
|
|
* Returns true if the string contains only alphabetic and numeric chars, |
805
|
|
|
* false otherwise. |
806
|
|
|
* |
807
|
|
|
* @return bool Whether or not $str contains only alphanumeric chars |
808
|
|
|
*/ |
809
|
13 |
|
public function isAlphanumeric() |
810
|
|
|
{ |
811
|
13 |
|
return $this->matchesPattern('^[[:alnum:]]*$'); |
812
|
|
|
} |
813
|
|
|
|
814
|
|
|
/** |
815
|
|
|
* Returns true if the string contains only whitespace chars, false |
816
|
|
|
* otherwise. |
817
|
|
|
* |
818
|
|
|
* @return bool Whether or not $str contains only whitespace characters |
819
|
|
|
*/ |
820
|
15 |
|
public function isBlank() |
821
|
|
|
{ |
822
|
15 |
|
return $this->matchesPattern('^[[:space:]]*$'); |
823
|
|
|
} |
824
|
|
|
|
825
|
|
|
/** |
826
|
|
|
* Returns true if the string contains only hexadecimal chars, false |
827
|
|
|
* otherwise. |
828
|
|
|
* |
829
|
|
|
* @return bool Whether or not $str contains only hexadecimal chars |
830
|
|
|
*/ |
831
|
13 |
|
public function isHexadecimal() |
832
|
|
|
{ |
833
|
13 |
|
return $this->matchesPattern('^[[:xdigit:]]*$'); |
834
|
|
|
} |
835
|
|
|
|
836
|
|
|
/** |
837
|
|
|
* Returns true if the string contains HTML-Tags, false otherwise. |
838
|
|
|
* |
839
|
|
|
* @return bool Whether or not $str contains HTML-Tags |
840
|
|
|
*/ |
841
|
1 |
|
public function isHtml() |
842
|
|
|
{ |
843
|
1 |
|
return UTF8::isHtml($this->str); |
|
|
|
|
844
|
|
|
} |
845
|
|
|
|
846
|
|
|
/** |
847
|
|
|
* Returns true if the string contains a valid E-Mail address, false otherwise. |
848
|
|
|
* |
849
|
|
|
* @param bool $useExampleDomainCheck |
850
|
|
|
* @param bool $useTypoInDomainCheck |
851
|
|
|
* @param bool $useTemporaryDomainCheck |
852
|
|
|
* @param bool $useDnsCheck |
853
|
|
|
* |
854
|
|
|
* @return bool Whether or not $str contains a valid E-Mail address |
855
|
|
|
*/ |
856
|
1 |
|
public function isEmail($useExampleDomainCheck = false, $useTypoInDomainCheck = false, $useTemporaryDomainCheck = false, $useDnsCheck = false) |
857
|
|
|
{ |
858
|
1 |
|
return EmailCheck::isValid($this->str, $useExampleDomainCheck, $useTypoInDomainCheck, $useTemporaryDomainCheck, $useDnsCheck); |
859
|
|
|
} |
860
|
|
|
|
861
|
|
|
/** |
862
|
|
|
* Returns true if the string is JSON, false otherwise. Unlike json_decode |
863
|
|
|
* in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers, |
864
|
|
|
* in that an empty string is not considered valid JSON. |
865
|
|
|
* |
866
|
|
|
* @return bool Whether or not $str is JSON |
867
|
|
|
*/ |
868
|
20 |
|
public function isJson() |
869
|
|
|
{ |
870
|
20 |
|
if (!isset($this->str[0])) { |
871
|
1 |
|
return false; |
872
|
|
|
} |
873
|
|
|
|
874
|
19 |
|
json_decode($this->str); |
875
|
|
|
|
876
|
19 |
|
if (json_last_error() === JSON_ERROR_NONE) { |
877
|
11 |
|
return true; |
878
|
|
|
} else { |
879
|
8 |
|
return false; |
880
|
|
|
} |
881
|
|
|
} |
882
|
|
|
|
883
|
|
|
/** |
884
|
|
|
* Returns true if the string contains only lower case chars, false |
885
|
|
|
* otherwise. |
886
|
|
|
* |
887
|
|
|
* @return bool Whether or not $str contains only lower case characters |
888
|
|
|
*/ |
889
|
8 |
|
public function isLowerCase() |
890
|
|
|
{ |
891
|
8 |
|
if ($this->matchesPattern('^[[:lower:]]*$')) { |
892
|
3 |
|
return true; |
893
|
|
|
} else { |
894
|
5 |
|
return false; |
895
|
|
|
} |
896
|
|
|
} |
897
|
|
|
|
898
|
|
|
/** |
899
|
|
|
* Returns true if the string is serialized, false otherwise. |
900
|
|
|
* |
901
|
|
|
* @return bool Whether or not $str is serialized |
902
|
|
|
*/ |
903
|
7 |
|
public function isSerialized() |
904
|
|
|
{ |
905
|
7 |
|
if (!isset($this->str[0])) { |
906
|
1 |
|
return false; |
907
|
|
|
} |
908
|
|
|
|
909
|
|
|
/** @noinspection PhpUsageOfSilenceOperatorInspection */ |
910
|
|
|
if ( |
911
|
6 |
|
$this->str === 'b:0;' |
912
|
|
|
|| |
913
|
6 |
|
@unserialize($this->str) !== false |
914
|
|
|
) { |
915
|
4 |
|
return true; |
916
|
|
|
} else { |
917
|
2 |
|
return false; |
918
|
|
|
} |
919
|
|
|
} |
920
|
|
|
|
921
|
|
|
/** |
922
|
|
|
* Returns true if the string contains only lower case chars, false |
923
|
|
|
* otherwise. |
924
|
|
|
* |
925
|
|
|
* @return bool Whether or not $str contains only lower case characters |
926
|
|
|
*/ |
927
|
8 |
|
public function isUpperCase() |
928
|
|
|
{ |
929
|
8 |
|
return $this->matchesPattern('^[[:upper:]]*$'); |
930
|
|
|
} |
931
|
|
|
|
932
|
|
|
/** |
933
|
|
|
* Returns the last $n characters of the string. |
934
|
|
|
* |
935
|
|
|
* @param int $n Number of characters to retrieve from the end |
936
|
|
|
* |
937
|
|
|
* @return Stringy Object with its $str being the last $n chars |
938
|
|
|
*/ |
939
|
12 |
View Code Duplication |
public function last($n) |
|
|
|
|
940
|
|
|
{ |
941
|
12 |
|
$stringy = static::create($this->str, $this->encoding); |
942
|
|
|
|
943
|
12 |
|
if ($n <= 0) { |
944
|
4 |
|
$stringy->str = ''; |
945
|
|
|
} else { |
946
|
8 |
|
return $stringy->substr(-$n); |
947
|
|
|
} |
948
|
|
|
|
949
|
4 |
|
return $stringy; |
950
|
|
|
} |
951
|
|
|
|
952
|
|
|
/** |
953
|
|
|
* Splits on newlines and carriage returns, returning an array of Stringy |
954
|
|
|
* objects corresponding to the lines in the string. |
955
|
|
|
* |
956
|
|
|
* @return Stringy[] An array of Stringy objects |
957
|
|
|
*/ |
958
|
15 |
|
public function lines() |
959
|
|
|
{ |
960
|
15 |
|
$array = preg_split('/[\r\n]{1,2}/u', $this->str); |
961
|
|
|
/** @noinspection CallableInLoopTerminationConditionInspection */ |
962
|
|
|
/** @noinspection ForeachInvariantsInspection */ |
963
|
15 |
View Code Duplication |
for ($i = 0; $i < count($array); $i++) { |
|
|
|
|
964
|
15 |
|
$array[$i] = static::create($array[$i], $this->encoding); |
965
|
|
|
} |
966
|
|
|
|
967
|
15 |
|
return $array; |
968
|
|
|
} |
969
|
|
|
|
970
|
|
|
/** |
971
|
|
|
* Returns the longest common prefix between the string and $otherStr. |
972
|
|
|
* |
973
|
|
|
* @param string $otherStr Second string for comparison |
974
|
|
|
* |
975
|
|
|
* @return Stringy Object with its $str being the longest common prefix |
976
|
|
|
*/ |
977
|
10 |
|
public function longestCommonPrefix($otherStr) |
978
|
|
|
{ |
979
|
10 |
|
$encoding = $this->encoding; |
980
|
10 |
|
$maxLength = min($this->length(), UTF8::strlen($otherStr, $encoding)); |
981
|
|
|
|
982
|
10 |
|
$longestCommonPrefix = ''; |
983
|
10 |
|
for ($i = 0; $i < $maxLength; $i++) { |
984
|
8 |
|
$char = UTF8::substr($this->str, $i, 1, $encoding); |
985
|
|
|
|
986
|
8 |
|
if ($char == UTF8::substr($otherStr, $i, 1, $encoding)) { |
987
|
6 |
|
$longestCommonPrefix .= $char; |
988
|
|
|
} else { |
989
|
6 |
|
break; |
990
|
|
|
} |
991
|
|
|
} |
992
|
|
|
|
993
|
10 |
|
return static::create($longestCommonPrefix, $encoding); |
994
|
|
|
} |
995
|
|
|
|
996
|
|
|
/** |
997
|
|
|
* Returns the longest common suffix between the string and $otherStr. |
998
|
|
|
* |
999
|
|
|
* @param string $otherStr Second string for comparison |
1000
|
|
|
* |
1001
|
|
|
* @return Stringy Object with its $str being the longest common suffix |
1002
|
|
|
*/ |
1003
|
10 |
|
public function longestCommonSuffix($otherStr) |
1004
|
|
|
{ |
1005
|
10 |
|
$encoding = $this->encoding; |
1006
|
10 |
|
$maxLength = min($this->length(), UTF8::strlen($otherStr, $encoding)); |
1007
|
|
|
|
1008
|
10 |
|
$longestCommonSuffix = ''; |
1009
|
10 |
|
for ($i = 1; $i <= $maxLength; $i++) { |
1010
|
8 |
|
$char = UTF8::substr($this->str, -$i, 1, $encoding); |
1011
|
|
|
|
1012
|
8 |
|
if ($char == UTF8::substr($otherStr, -$i, 1, $encoding)) { |
1013
|
6 |
|
$longestCommonSuffix = $char . $longestCommonSuffix; |
1014
|
|
|
} else { |
1015
|
6 |
|
break; |
1016
|
|
|
} |
1017
|
|
|
} |
1018
|
|
|
|
1019
|
10 |
|
return static::create($longestCommonSuffix, $encoding); |
1020
|
|
|
} |
1021
|
|
|
|
1022
|
|
|
/** |
1023
|
|
|
* Returns the longest common substring between the string and $otherStr. |
1024
|
|
|
* In the case of ties, it returns that which occurs first. |
1025
|
|
|
* |
1026
|
|
|
* @param string $otherStr Second string for comparison |
1027
|
|
|
* |
1028
|
|
|
* @return Stringy Object with its $str being the longest common substring |
1029
|
|
|
*/ |
1030
|
10 |
|
public function longestCommonSubstring($otherStr) |
1031
|
|
|
{ |
1032
|
|
|
// Uses dynamic programming to solve |
1033
|
|
|
// http://en.wikipedia.org/wiki/Longest_common_substring_problem |
1034
|
10 |
|
$encoding = $this->encoding; |
1035
|
10 |
|
$stringy = static::create($this->str, $encoding); |
1036
|
10 |
|
$strLength = $stringy->length(); |
1037
|
10 |
|
$otherLength = UTF8::strlen($otherStr, $encoding); |
1038
|
|
|
|
1039
|
|
|
// Return if either string is empty |
1040
|
10 |
|
if ($strLength == 0 || $otherLength == 0) { |
1041
|
2 |
|
$stringy->str = ''; |
1042
|
|
|
|
1043
|
2 |
|
return $stringy; |
1044
|
|
|
} |
1045
|
|
|
|
1046
|
8 |
|
$len = 0; |
1047
|
8 |
|
$end = 0; |
1048
|
8 |
|
$table = array_fill( |
1049
|
8 |
|
0, $strLength + 1, |
1050
|
8 |
|
array_fill(0, $otherLength + 1, 0) |
1051
|
|
|
); |
1052
|
|
|
|
1053
|
8 |
|
for ($i = 1; $i <= $strLength; $i++) { |
1054
|
8 |
|
for ($j = 1; $j <= $otherLength; $j++) { |
1055
|
8 |
|
$strChar = UTF8::substr($stringy->str, $i - 1, 1, $encoding); |
1056
|
8 |
|
$otherChar = UTF8::substr($otherStr, $j - 1, 1, $encoding); |
1057
|
|
|
|
1058
|
8 |
|
if ($strChar == $otherChar) { |
1059
|
8 |
|
$table[$i][$j] = $table[$i - 1][$j - 1] + 1; |
1060
|
8 |
|
if ($table[$i][$j] > $len) { |
1061
|
8 |
|
$len = $table[$i][$j]; |
1062
|
8 |
|
$end = $i; |
1063
|
|
|
} |
1064
|
|
|
} else { |
1065
|
8 |
|
$table[$i][$j] = 0; |
1066
|
|
|
} |
1067
|
|
|
} |
1068
|
|
|
} |
1069
|
|
|
|
1070
|
8 |
|
$stringy->str = UTF8::substr($stringy->str, $end - $len, $len, $encoding); |
|
|
|
|
1071
|
|
|
|
1072
|
8 |
|
return $stringy; |
1073
|
|
|
} |
1074
|
|
|
|
1075
|
|
|
/** |
1076
|
|
|
* Returns whether or not a character exists at an index. Offsets may be |
1077
|
|
|
* negative to count from the last character in the string. Implements |
1078
|
|
|
* part of the ArrayAccess interface. |
1079
|
|
|
* |
1080
|
|
|
* @param mixed $offset The index to check |
1081
|
|
|
* |
1082
|
|
|
* @return boolean Whether or not the index exists |
1083
|
|
|
*/ |
1084
|
6 |
|
public function offsetExists($offset) |
1085
|
|
|
{ |
1086
|
|
|
// init |
1087
|
6 |
|
$length = $this->length(); |
1088
|
6 |
|
$offset = (int)$offset; |
1089
|
|
|
|
1090
|
6 |
|
if ($offset >= 0) { |
1091
|
3 |
|
return ($length > $offset); |
1092
|
|
|
} |
1093
|
|
|
|
1094
|
3 |
|
return ($length >= abs($offset)); |
1095
|
|
|
} |
1096
|
|
|
|
1097
|
|
|
/** |
1098
|
|
|
* Returns the character at the given index. Offsets may be negative to |
1099
|
|
|
* count from the last character in the string. Implements part of the |
1100
|
|
|
* ArrayAccess interface, and throws an OutOfBoundsException if the index |
1101
|
|
|
* does not exist. |
1102
|
|
|
* |
1103
|
|
|
* @param mixed $offset The index from which to retrieve the char |
1104
|
|
|
* |
1105
|
|
|
* @return string The character at the specified index |
1106
|
|
|
* @throws \OutOfBoundsException If the positive or negative offset does |
1107
|
|
|
* not exist |
1108
|
|
|
*/ |
1109
|
2 |
|
public function offsetGet($offset) |
1110
|
|
|
{ |
1111
|
|
|
// init |
1112
|
2 |
|
$offset = (int)$offset; |
1113
|
2 |
|
$length = $this->length(); |
1114
|
|
|
|
1115
|
|
|
if ( |
1116
|
2 |
|
($offset >= 0 && $length <= $offset) |
1117
|
|
|
|| |
1118
|
2 |
|
$length < abs($offset) |
1119
|
|
|
) { |
1120
|
1 |
|
throw new \OutOfBoundsException('No character exists at the index'); |
1121
|
|
|
} |
1122
|
|
|
|
1123
|
1 |
|
return UTF8::substr($this->str, $offset, 1, $this->encoding); |
1124
|
|
|
} |
1125
|
|
|
|
1126
|
|
|
/** |
1127
|
|
|
* Implements part of the ArrayAccess interface, but throws an exception |
1128
|
|
|
* when called. This maintains the immutability of Stringy objects. |
1129
|
|
|
* |
1130
|
|
|
* @param mixed $offset The index of the character |
1131
|
|
|
* @param mixed $value Value to set |
1132
|
|
|
* |
1133
|
|
|
* @throws \Exception When called |
1134
|
|
|
*/ |
1135
|
1 |
|
public function offsetSet($offset, $value) |
1136
|
|
|
{ |
1137
|
|
|
// Stringy is immutable, cannot directly set char |
1138
|
1 |
|
throw new \Exception('Stringy object is immutable, cannot modify char'); |
1139
|
|
|
} |
1140
|
|
|
|
1141
|
|
|
/** |
1142
|
|
|
* Implements part of the ArrayAccess interface, but throws an exception |
1143
|
|
|
* when called. This maintains the immutability of Stringy objects. |
1144
|
|
|
* |
1145
|
|
|
* @param mixed $offset The index of the character |
1146
|
|
|
* |
1147
|
|
|
* @throws \Exception When called |
1148
|
|
|
*/ |
1149
|
1 |
|
public function offsetUnset($offset) |
1150
|
|
|
{ |
1151
|
|
|
// Don't allow directly modifying the string |
1152
|
1 |
|
throw new \Exception('Stringy object is immutable, cannot unset char'); |
1153
|
|
|
} |
1154
|
|
|
|
1155
|
|
|
/** |
1156
|
|
|
* Pads the string to a given length with $padStr. If length is less than |
1157
|
|
|
* or equal to the length of the string, no padding takes places. The |
1158
|
|
|
* default string used for padding is a space, and the default type (one of |
1159
|
|
|
* 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException |
1160
|
|
|
* if $padType isn't one of those 3 values. |
1161
|
|
|
* |
1162
|
|
|
* @param int $length Desired string length after padding |
1163
|
|
|
* @param string $padStr String used to pad, defaults to space |
1164
|
|
|
* @param string $padType One of 'left', 'right', 'both' |
1165
|
|
|
* |
1166
|
|
|
* @return Stringy Object with a padded $str |
1167
|
|
|
* @throws \InvalidArgumentException If $padType isn't one of 'right', 'left' or 'both' |
1168
|
|
|
*/ |
1169
|
13 |
|
public function pad($length, $padStr = ' ', $padType = 'right') |
1170
|
|
|
{ |
1171
|
13 |
|
if (!in_array($padType, array('left', 'right', 'both'), true)) { |
1172
|
1 |
|
throw new \InvalidArgumentException( |
1173
|
1 |
|
'Pad expects $padType ' . "to be one of 'left', 'right' or 'both'" |
1174
|
|
|
); |
1175
|
|
|
} |
1176
|
|
|
|
1177
|
|
|
switch ($padType) { |
1178
|
12 |
|
case 'left': |
1179
|
3 |
|
return $this->padLeft($length, $padStr); |
1180
|
9 |
|
case 'right': |
1181
|
6 |
|
return $this->padRight($length, $padStr); |
1182
|
|
|
default: |
1183
|
3 |
|
return $this->padBoth($length, $padStr); |
1184
|
|
|
} |
1185
|
|
|
} |
1186
|
|
|
|
1187
|
|
|
/** |
1188
|
|
|
* Returns a new string of a given length such that the beginning of the |
1189
|
|
|
* string is padded. Alias for pad() with a $padType of 'left'. |
1190
|
|
|
* |
1191
|
|
|
* @param int $length Desired string length after padding |
1192
|
|
|
* @param string $padStr String used to pad, defaults to space |
1193
|
|
|
* |
1194
|
|
|
* @return Stringy String with left padding |
1195
|
|
|
*/ |
1196
|
10 |
|
public function padLeft($length, $padStr = ' ') |
1197
|
|
|
{ |
1198
|
10 |
|
return $this->applyPadding($length - $this->length(), 0, $padStr); |
1199
|
|
|
} |
1200
|
|
|
|
1201
|
|
|
/** |
1202
|
|
|
* Adds the specified amount of left and right padding to the given string. |
1203
|
|
|
* The default character used is a space. |
1204
|
|
|
* |
1205
|
|
|
* @param int $left Length of left padding |
1206
|
|
|
* @param int $right Length of right padding |
1207
|
|
|
* @param string $padStr String used to pad |
1208
|
|
|
* |
1209
|
|
|
* @return Stringy String with padding applied |
1210
|
|
|
*/ |
1211
|
37 |
|
private function applyPadding($left = 0, $right = 0, $padStr = ' ') |
1212
|
|
|
{ |
1213
|
37 |
|
$stringy = static::create($this->str, $this->encoding); |
1214
|
|
|
|
1215
|
37 |
|
$length = UTF8::strlen($padStr, $stringy->encoding); |
1216
|
|
|
|
1217
|
37 |
|
$strLength = $stringy->length(); |
1218
|
37 |
|
$paddedLength = $strLength + $left + $right; |
1219
|
|
|
|
1220
|
37 |
|
if (!$length || $paddedLength <= $strLength) { |
1221
|
3 |
|
return $stringy; |
1222
|
|
|
} |
1223
|
|
|
|
1224
|
34 |
|
$leftPadding = UTF8::substr( |
1225
|
34 |
|
UTF8::str_repeat( |
1226
|
|
|
$padStr, |
1227
|
34 |
|
ceil($left / $length) |
1228
|
|
|
), |
1229
|
34 |
|
0, |
1230
|
|
|
$left, |
1231
|
34 |
|
$stringy->encoding |
1232
|
|
|
); |
1233
|
|
|
|
1234
|
34 |
|
$rightPadding = UTF8::substr( |
1235
|
34 |
|
UTF8::str_repeat( |
1236
|
|
|
$padStr, |
1237
|
34 |
|
ceil($right / $length) |
1238
|
|
|
), |
1239
|
34 |
|
0, |
1240
|
|
|
$right, |
1241
|
34 |
|
$stringy->encoding |
1242
|
|
|
); |
1243
|
|
|
|
1244
|
34 |
|
$stringy->str = $leftPadding . $stringy->str . $rightPadding; |
1245
|
|
|
|
1246
|
34 |
|
return $stringy; |
1247
|
|
|
} |
1248
|
|
|
|
1249
|
|
|
/** |
1250
|
|
|
* Returns a new string of a given length such that the end of the string |
1251
|
|
|
* is padded. Alias for pad() with a $padType of 'right'. |
1252
|
|
|
* |
1253
|
|
|
* @param int $length Desired string length after padding |
1254
|
|
|
* @param string $padStr String used to pad, defaults to space |
1255
|
|
|
* |
1256
|
|
|
* @return Stringy String with right padding |
1257
|
|
|
*/ |
1258
|
13 |
|
public function padRight($length, $padStr = ' ') |
1259
|
|
|
{ |
1260
|
13 |
|
return $this->applyPadding(0, $length - $this->length(), $padStr); |
1261
|
|
|
} |
1262
|
|
|
|
1263
|
|
|
/** |
1264
|
|
|
* Returns a new string of a given length such that both sides of the |
1265
|
|
|
* string are padded. Alias for pad() with a $padType of 'both'. |
1266
|
|
|
* |
1267
|
|
|
* @param int $length Desired string length after padding |
1268
|
|
|
* @param string $padStr String used to pad, defaults to space |
1269
|
|
|
* |
1270
|
|
|
* @return Stringy String with padding applied |
1271
|
|
|
*/ |
1272
|
14 |
|
public function padBoth($length, $padStr = ' ') |
1273
|
|
|
{ |
1274
|
14 |
|
$padding = $length - $this->length(); |
1275
|
|
|
|
1276
|
14 |
|
return $this->applyPadding(floor($padding / 2), ceil($padding / 2), $padStr); |
1277
|
|
|
} |
1278
|
|
|
|
1279
|
|
|
/** |
1280
|
|
|
* Returns a new string starting with $string. |
1281
|
|
|
* |
1282
|
|
|
* @param string $string The string to append |
1283
|
|
|
* |
1284
|
|
|
* @return Stringy Object with appended $string |
1285
|
|
|
*/ |
1286
|
2 |
|
public function prepend($string) |
1287
|
|
|
{ |
1288
|
2 |
|
return static::create($string . $this->str, $this->encoding); |
1289
|
|
|
} |
1290
|
|
|
|
1291
|
|
|
/** |
1292
|
|
|
* Returns a new string with the prefix $substring removed, if present. |
1293
|
|
|
* |
1294
|
|
|
* @param string $substring The prefix to remove |
1295
|
|
|
* |
1296
|
|
|
* @return Stringy Object having a $str without the prefix $substring |
1297
|
|
|
*/ |
1298
|
12 |
View Code Duplication |
public function removeLeft($substring) |
|
|
|
|
1299
|
|
|
{ |
1300
|
12 |
|
$stringy = static::create($this->str, $this->encoding); |
1301
|
|
|
|
1302
|
12 |
|
if ($stringy->startsWith($substring)) { |
1303
|
6 |
|
$substringLength = UTF8::strlen($substring, $stringy->encoding); |
1304
|
|
|
|
1305
|
6 |
|
return $stringy->substr($substringLength); |
1306
|
|
|
} |
1307
|
|
|
|
1308
|
6 |
|
return $stringy; |
1309
|
|
|
} |
1310
|
|
|
|
1311
|
|
|
/** |
1312
|
|
|
* Returns a new string with the suffix $substring removed, if present. |
1313
|
|
|
* |
1314
|
|
|
* @param string $substring The suffix to remove |
1315
|
|
|
* |
1316
|
|
|
* @return Stringy Object having a $str without the suffix $substring |
1317
|
|
|
*/ |
1318
|
12 |
View Code Duplication |
public function removeRight($substring) |
|
|
|
|
1319
|
|
|
{ |
1320
|
12 |
|
$stringy = static::create($this->str, $this->encoding); |
1321
|
|
|
|
1322
|
12 |
|
if ($stringy->endsWith($substring)) { |
1323
|
8 |
|
$substringLength = UTF8::strlen($substring, $stringy->encoding); |
1324
|
|
|
|
1325
|
8 |
|
return $stringy->substr(0, $stringy->length() - $substringLength); |
1326
|
|
|
} |
1327
|
|
|
|
1328
|
4 |
|
return $stringy; |
1329
|
|
|
} |
1330
|
|
|
|
1331
|
|
|
/** |
1332
|
|
|
* Returns a repeated string given a multiplier. |
1333
|
|
|
* |
1334
|
|
|
* @param int $multiplier The number of times to repeat the string |
1335
|
|
|
* |
1336
|
|
|
* @return Stringy Object with a repeated str |
1337
|
|
|
*/ |
1338
|
7 |
|
public function repeat($multiplier) |
1339
|
|
|
{ |
1340
|
7 |
|
$repeated = UTF8::str_repeat($this->str, $multiplier); |
1341
|
|
|
|
1342
|
7 |
|
return static::create($repeated, $this->encoding); |
1343
|
|
|
} |
1344
|
|
|
|
1345
|
|
|
/** |
1346
|
|
|
* Replaces all occurrences of $search in $str by $replacement. |
1347
|
|
|
* |
1348
|
|
|
* @param string $search The needle to search for |
1349
|
|
|
* @param string $replacement The string to replace with |
1350
|
|
|
* @param bool $caseSensitive Whether or not to enforce case-sensitivity |
1351
|
|
|
* |
1352
|
|
|
* @return Stringy Object with the resulting $str after the replacements |
1353
|
|
|
*/ |
1354
|
28 |
View Code Duplication |
public function replace($search, $replacement, $caseSensitive = true) |
|
|
|
|
1355
|
|
|
{ |
1356
|
28 |
|
if ($caseSensitive) { |
1357
|
21 |
|
$return = UTF8::str_replace($search, $replacement, $this->str); |
1358
|
|
|
} else { |
1359
|
7 |
|
$return = UTF8::str_ireplace($search, $replacement, $this->str); |
1360
|
|
|
} |
1361
|
|
|
|
1362
|
28 |
|
return static::create($return); |
1363
|
|
|
} |
1364
|
|
|
|
1365
|
|
|
/** |
1366
|
|
|
* Replaces all occurrences of $search in $str by $replacement. |
1367
|
|
|
* |
1368
|
|
|
* @param array $search The elements to search for |
1369
|
|
|
* @param string|array $replacement The string to replace with |
1370
|
|
|
* @param bool $caseSensitive Whether or not to enforce case-sensitivity |
1371
|
|
|
* |
1372
|
|
|
* @return Stringy Object with the resulting $str after the replacements |
1373
|
|
|
*/ |
1374
|
30 |
View Code Duplication |
public function replaceAll(array $search, $replacement, $caseSensitive = true) |
|
|
|
|
1375
|
|
|
{ |
1376
|
30 |
|
if ($caseSensitive) { |
1377
|
23 |
|
$return = UTF8::str_replace($search, $replacement, $this->str); |
1378
|
|
|
} else { |
1379
|
7 |
|
$return = UTF8::str_ireplace($search, $replacement, $this->str); |
1380
|
|
|
} |
1381
|
|
|
|
1382
|
30 |
|
return static::create($return); |
1383
|
|
|
} |
1384
|
|
|
|
1385
|
|
|
/** |
1386
|
|
|
* Replaces all occurrences of $search from the beginning of string with $replacement |
1387
|
|
|
* |
1388
|
|
|
* @param string $search |
1389
|
|
|
* @param string $replacement |
1390
|
|
|
* |
1391
|
|
|
* @return Stringy Object with the resulting $str after the replacements |
1392
|
|
|
*/ |
1393
|
16 |
|
public function replaceBeginning($search, $replacement) |
1394
|
|
|
{ |
1395
|
16 |
|
$str = $this->regexReplace('^' . preg_quote($search, '/'), UTF8::str_replace('\\', '\\\\', $replacement)); |
1396
|
|
|
|
1397
|
16 |
|
return static::create($str, $this->encoding); |
1398
|
|
|
} |
1399
|
|
|
|
1400
|
|
|
/** |
1401
|
|
|
* Replaces all occurrences of $search from the ending of string with $replacement |
1402
|
|
|
* |
1403
|
|
|
* @param string $search |
1404
|
|
|
* @param string $replacement |
1405
|
|
|
* |
1406
|
|
|
* @return Stringy Object with the resulting $str after the replacements |
1407
|
|
|
*/ |
1408
|
16 |
|
public function replaceEnding($search, $replacement) |
1409
|
|
|
{ |
1410
|
16 |
|
$str = $this->regexReplace(preg_quote($search, '/') . '$', UTF8::str_replace('\\', '\\\\', $replacement)); |
1411
|
|
|
|
1412
|
16 |
|
return static::create($str, $this->encoding); |
1413
|
|
|
} |
1414
|
|
|
|
1415
|
|
|
/** |
1416
|
|
|
* Returns a reversed string. A multibyte version of strrev(). |
1417
|
|
|
* |
1418
|
|
|
* @return Stringy Object with a reversed $str |
1419
|
|
|
*/ |
1420
|
5 |
|
public function reverse() |
1421
|
|
|
{ |
1422
|
5 |
|
$reversed = UTF8::strrev($this->str); |
1423
|
|
|
|
1424
|
5 |
|
return static::create($reversed, $this->encoding); |
1425
|
|
|
} |
1426
|
|
|
|
1427
|
|
|
/** |
1428
|
|
|
* Truncates the string to a given length, while ensuring that it does not |
1429
|
|
|
* split words. If $substring is provided, and truncating occurs, the |
1430
|
|
|
* string is further truncated so that the substring may be appended without |
1431
|
|
|
* exceeding the desired length. |
1432
|
|
|
* |
1433
|
|
|
* @param int $length Desired length of the truncated string |
1434
|
|
|
* @param string $substring The substring to append if it can fit |
1435
|
|
|
* |
1436
|
|
|
* @return Stringy Object with the resulting $str after truncating |
1437
|
|
|
*/ |
1438
|
23 |
|
public function safeTruncate($length, $substring = '') |
1439
|
|
|
{ |
1440
|
23 |
|
$stringy = static::create($this->str, $this->encoding); |
1441
|
23 |
|
if ($length >= $stringy->length()) { |
1442
|
4 |
|
return $stringy; |
1443
|
|
|
} |
1444
|
|
|
|
1445
|
|
|
// Need to further trim the string so we can append the substring |
1446
|
19 |
|
$encoding = $stringy->encoding; |
1447
|
19 |
|
$substringLength = UTF8::strlen($substring, $encoding); |
1448
|
19 |
|
$length -= $substringLength; |
1449
|
|
|
|
1450
|
19 |
|
$truncated = UTF8::substr($stringy->str, 0, $length, $encoding); |
1451
|
|
|
|
1452
|
|
|
// If the last word was truncated |
1453
|
19 |
|
$strPosSpace = UTF8::strpos($stringy->str, ' ', $length - 1, $encoding); |
1454
|
19 |
|
if ($strPosSpace != $length) { |
1455
|
|
|
// Find pos of the last occurrence of a space, get up to that |
1456
|
12 |
|
$lastPos = UTF8::strrpos($truncated, ' ', 0, $encoding); |
|
|
|
|
1457
|
|
|
|
1458
|
12 |
|
if ($lastPos !== false || $strPosSpace !== false) { |
1459
|
11 |
|
$truncated = UTF8::substr($truncated, 0, $lastPos, $encoding); |
|
|
|
|
1460
|
|
|
} |
1461
|
|
|
} |
1462
|
|
|
|
1463
|
19 |
|
$stringy->str = $truncated . $substring; |
1464
|
|
|
|
1465
|
19 |
|
return $stringy; |
1466
|
|
|
} |
1467
|
|
|
|
1468
|
|
|
/** |
1469
|
|
|
* A multibyte string shuffle function. It returns a string with its |
1470
|
|
|
* characters in random order. |
1471
|
|
|
* |
1472
|
|
|
* @return Stringy Object with a shuffled $str |
1473
|
|
|
*/ |
1474
|
3 |
|
public function shuffle() |
1475
|
|
|
{ |
1476
|
3 |
|
$shuffledStr = UTF8::str_shuffle($this->str); |
1477
|
|
|
|
1478
|
3 |
|
return static::create($shuffledStr, $this->encoding); |
1479
|
|
|
} |
1480
|
|
|
|
1481
|
|
|
/** |
1482
|
|
|
* Converts the string into an URL slug. This includes replacing non-ASCII |
1483
|
|
|
* characters with their closest ASCII equivalents, removing remaining |
1484
|
|
|
* non-ASCII and non-alphanumeric characters, and replacing whitespace with |
1485
|
|
|
* $replacement. The replacement defaults to a single dash, and the string |
1486
|
|
|
* is also converted to lowercase. |
1487
|
|
|
* |
1488
|
|
|
* @param string $replacement The string used to replace whitespace |
1489
|
|
|
* @param string $language The language for the url |
1490
|
|
|
* @param bool $strToLower string to lower |
1491
|
|
|
* |
1492
|
|
|
* @return Stringy Object whose $str has been converted to an URL slug |
1493
|
|
|
*/ |
1494
|
15 |
|
public function slugify($replacement = '-', $language = 'de', $strToLower = true) |
1495
|
|
|
{ |
1496
|
15 |
|
$slug = URLify::slug($this->str, $language, $replacement, $strToLower); |
1497
|
|
|
|
1498
|
15 |
|
return static::create($slug, $this->encoding); |
1499
|
|
|
} |
1500
|
|
|
|
1501
|
|
|
/** |
1502
|
|
|
* Remove css media-queries. |
1503
|
|
|
* |
1504
|
|
|
* @return Stringy |
1505
|
|
|
*/ |
1506
|
1 |
|
public function stripeCssMediaQueries() |
1507
|
|
|
{ |
1508
|
1 |
|
$pattern = '#@media\\s+(?:only\\s)?(?:[\\s{\\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU'; |
1509
|
|
|
|
1510
|
1 |
|
return static::create(preg_replace($pattern, '', $this->str)); |
1511
|
|
|
} |
1512
|
|
|
|
1513
|
|
|
/** |
1514
|
|
|
* Remove empty html-tag. |
1515
|
|
|
* |
1516
|
|
|
* e.g.: <tag></tag> |
1517
|
|
|
* |
1518
|
|
|
* @return Stringy |
1519
|
|
|
*/ |
1520
|
1 |
|
public function stripeEmptyHtmlTags() |
1521
|
|
|
{ |
1522
|
1 |
|
$pattern = "/<[^\/>]*>(([\s]?)*|)<\/[^>]*>/i"; |
1523
|
|
|
|
1524
|
1 |
|
return static::create(preg_replace($pattern, '', $this->str)); |
1525
|
|
|
} |
1526
|
|
|
|
1527
|
|
|
/** |
1528
|
|
|
* Converts the string into an valid UTF-8 string. |
1529
|
|
|
* |
1530
|
|
|
* @return Stringy |
1531
|
|
|
*/ |
1532
|
1 |
|
public function utf8ify() |
1533
|
|
|
{ |
1534
|
1 |
|
return static::create(UTF8::cleanup($this->str)); |
1535
|
|
|
} |
1536
|
|
|
|
1537
|
|
|
/** |
1538
|
|
|
* escape html |
1539
|
|
|
* |
1540
|
|
|
* @return Stringy |
1541
|
|
|
*/ |
1542
|
6 |
|
public function escape() |
1543
|
|
|
{ |
1544
|
6 |
|
$str = UTF8::htmlspecialchars( |
1545
|
6 |
|
$this->str, |
1546
|
6 |
|
ENT_QUOTES | ENT_SUBSTITUTE, |
1547
|
6 |
|
$this->encoding |
1548
|
|
|
); |
1549
|
|
|
|
1550
|
6 |
|
return static::create($str, $this->encoding); |
1551
|
|
|
} |
1552
|
|
|
|
1553
|
|
|
/** |
1554
|
|
|
* Create an extract from a text, so if the search-string was found, it will be centered in the output. |
1555
|
|
|
* |
1556
|
|
|
* @param string $search |
1557
|
|
|
* @param int|null $length |
1558
|
|
|
* @param string $ellipsis |
1559
|
|
|
* |
1560
|
|
|
* @return Stringy |
1561
|
|
|
*/ |
1562
|
1 |
|
public function extractText($search = '', $length = null, $ellipsis = '...') |
1563
|
|
|
{ |
1564
|
|
|
// init |
1565
|
1 |
|
$text = $this->str; |
1566
|
|
|
|
1567
|
1 |
|
if (empty($text)) { |
1568
|
1 |
|
return static::create('', $this->encoding); |
1569
|
|
|
} |
1570
|
|
|
|
1571
|
1 |
|
$trimChars = "\t\r\n -_()!~?=+/*\\,.:;\"'[]{}`&"; |
1572
|
|
|
|
1573
|
1 |
|
if ($length === null) { |
1574
|
1 |
|
$length = $this->length() / 2; |
1575
|
|
|
} |
1576
|
|
|
|
1577
|
1 |
|
if (empty($search)) { |
1578
|
|
|
|
1579
|
|
|
$stringLength = UTF8::strlen($text, $this->encoding); |
1580
|
|
|
$end = ($length - 1) > $stringLength ? $stringLength : ($length - 1); |
1581
|
|
|
$pos = min(UTF8::strpos($text, ' ', $end, 0, $this->encoding), UTF8::strpos($text, '.', $end)); |
|
|
|
|
1582
|
|
|
|
1583
|
|
|
if ($pos) { |
1584
|
|
|
return static::create( |
1585
|
|
|
rtrim( |
1586
|
|
|
UTF8::substr($text, 0, $pos, $this->encoding), |
1587
|
|
|
$trimChars |
1588
|
|
|
) . $ellipsis, |
1589
|
|
|
$this->encoding |
1590
|
|
|
); |
1591
|
|
|
} else { |
1592
|
|
|
return static::create($text, $this->encoding); |
1593
|
|
|
} |
1594
|
|
|
|
1595
|
|
|
} |
1596
|
|
|
|
1597
|
1 |
|
$wordPos = UTF8::strpos( |
1598
|
1 |
|
UTF8::strtolower($text), |
1599
|
1 |
|
UTF8::strtolower($search, $this->encoding), |
1600
|
1 |
|
null, |
1601
|
1 |
|
$this->encoding |
1602
|
|
|
); |
1603
|
1 |
|
$halfSide = (int)($wordPos - $length / 2 + UTF8::strlen($search, $this->encoding) / 2); |
1604
|
|
|
|
1605
|
1 |
|
if ($halfSide > 0) { |
1606
|
|
|
|
1607
|
1 |
|
$halfText = UTF8::substr($text, 0, $halfSide, $this->encoding); |
1608
|
1 |
|
$pos_start = max(UTF8::strrpos($halfText, ' ', 0), UTF8::strrpos($halfText, '.', 0)); |
|
|
|
|
1609
|
|
|
|
1610
|
1 |
|
if (!$pos_start) { |
1611
|
1 |
|
$pos_start = 0; |
1612
|
|
|
} |
1613
|
|
|
|
1614
|
|
|
} else { |
1615
|
1 |
|
$pos_start = 0; |
1616
|
|
|
} |
1617
|
|
|
|
1618
|
1 |
|
if ($wordPos && $halfSide > 0) { |
1619
|
1 |
|
$l = $pos_start + $length - 1; |
1620
|
1 |
|
$realLength = UTF8::strlen($text, $this->encoding); |
1621
|
|
|
|
1622
|
1 |
|
if ($l > $realLength) { |
1623
|
|
|
$l = $realLength; |
1624
|
|
|
} |
1625
|
|
|
|
1626
|
1 |
|
$pos_end = min( |
1627
|
1 |
|
UTF8::strpos($text, ' ', $l, $this->encoding), |
1628
|
1 |
|
UTF8::strpos($text, '.', $l, $this->encoding) |
1629
|
1 |
|
) - $pos_start; |
1630
|
|
|
|
1631
|
1 |
|
if (!$pos_end || $pos_end <= 0) { |
1632
|
1 |
|
$extract = $ellipsis . ltrim( |
1633
|
1 |
|
UTF8::substr( |
1634
|
|
|
$text, |
1635
|
|
|
$pos_start, |
1636
|
1 |
|
UTF8::strlen($text), |
1637
|
1 |
|
$this->encoding |
1638
|
|
|
), |
1639
|
|
|
$trimChars |
1640
|
|
|
); |
1641
|
|
View Code Duplication |
} else { |
|
|
|
|
1642
|
1 |
|
$extract = $ellipsis . trim( |
1643
|
1 |
|
UTF8::substr( |
1644
|
|
|
$text, |
1645
|
|
|
$pos_start, |
1646
|
|
|
$pos_end, |
1647
|
1 |
|
$this->encoding |
1648
|
|
|
), |
1649
|
|
|
$trimChars |
1650
|
1 |
|
) . $ellipsis; |
1651
|
|
|
} |
1652
|
|
|
|
1653
|
|
|
} else { |
1654
|
|
|
|
1655
|
1 |
|
$l = $length - 1; |
1656
|
1 |
|
$trueLength = UTF8::strlen($text, $this->encoding); |
1657
|
|
|
|
1658
|
1 |
|
if ($l > $trueLength) { |
1659
|
|
|
$l = $trueLength; |
1660
|
|
|
} |
1661
|
|
|
|
1662
|
1 |
|
$pos_end = min( |
1663
|
1 |
|
UTF8::strpos($text, ' ', $l, $this->encoding), |
1664
|
1 |
|
UTF8::strpos($text, '.', $l, $this->encoding) |
1665
|
|
|
); |
1666
|
|
|
|
1667
|
1 |
View Code Duplication |
if ($pos_end) { |
|
|
|
|
1668
|
1 |
|
$extract = rtrim( |
1669
|
1 |
|
UTF8::substr($text, 0, $pos_end, $this->encoding), |
1670
|
|
|
$trimChars |
1671
|
1 |
|
) . $ellipsis; |
1672
|
|
|
} else { |
1673
|
1 |
|
$extract = $text; |
1674
|
|
|
} |
1675
|
|
|
} |
1676
|
|
|
|
1677
|
1 |
|
return static::create($extract, $this->encoding); |
1678
|
|
|
} |
1679
|
|
|
|
1680
|
|
|
|
1681
|
|
|
/** |
1682
|
|
|
* remove xss from html |
1683
|
|
|
* |
1684
|
|
|
* @return Stringy |
1685
|
|
|
*/ |
1686
|
6 |
|
public function removeXss() |
1687
|
|
|
{ |
1688
|
6 |
|
static $antiXss = null; |
1689
|
|
|
|
1690
|
6 |
|
if ($antiXss === null) { |
1691
|
1 |
|
$antiXss = new AntiXSS(); |
1692
|
|
|
} |
1693
|
|
|
|
1694
|
6 |
|
$str = $antiXss->xss_clean($this->str); |
1695
|
|
|
|
1696
|
6 |
|
return static::create($str, $this->encoding); |
1697
|
|
|
} |
1698
|
|
|
|
1699
|
|
|
/** |
1700
|
|
|
* remove html-break [br | \r\n | \r | \n | ...] |
1701
|
|
|
* |
1702
|
|
|
* @param string $replacement |
1703
|
|
|
* |
1704
|
|
|
* @return Stringy |
1705
|
|
|
*/ |
1706
|
6 |
|
public function removeHtmlBreak($replacement = '') |
1707
|
|
|
{ |
1708
|
6 |
|
$str = preg_replace('#/\r\n|\r|\n|<br.*/?>#isU', $replacement, $this->str); |
1709
|
|
|
|
1710
|
6 |
|
return static::create($str, $this->encoding); |
1711
|
|
|
} |
1712
|
|
|
|
1713
|
|
|
/** |
1714
|
|
|
* remove html |
1715
|
|
|
* |
1716
|
|
|
* @param $allowableTags |
1717
|
|
|
* |
1718
|
|
|
* @return Stringy |
1719
|
|
|
*/ |
1720
|
6 |
|
public function removeHtml($allowableTags = null) |
1721
|
|
|
{ |
1722
|
6 |
|
$str = strip_tags($this->str, $allowableTags); |
1723
|
|
|
|
1724
|
6 |
|
return static::create($str, $this->encoding); |
1725
|
|
|
} |
1726
|
|
|
|
1727
|
|
|
/** |
1728
|
|
|
* Returns the substring beginning at $start, and up to, but not including |
1729
|
|
|
* the index specified by $end. If $end is omitted, the function extracts |
1730
|
|
|
* the remaining string. If $end is negative, it is computed from the end |
1731
|
|
|
* of the string. |
1732
|
|
|
* |
1733
|
|
|
* @param int $start Initial index from which to begin extraction |
1734
|
|
|
* @param int $end Optional index at which to end extraction |
1735
|
|
|
* |
1736
|
|
|
* @return Stringy Object with its $str being the extracted substring |
1737
|
|
|
*/ |
1738
|
18 |
|
public function slice($start, $end = null) |
1739
|
|
|
{ |
1740
|
18 |
|
if ($end === null) { |
1741
|
4 |
|
$length = $this->length(); |
1742
|
14 |
|
} elseif ($end >= 0 && $end <= $start) { |
1743
|
4 |
|
return static::create('', $this->encoding); |
1744
|
10 |
|
} elseif ($end < 0) { |
1745
|
2 |
|
$length = $this->length() + $end - $start; |
1746
|
|
|
} else { |
1747
|
8 |
|
$length = $end - $start; |
1748
|
|
|
} |
1749
|
|
|
|
1750
|
14 |
|
$str = UTF8::substr($this->str, $start, $length, $this->encoding); |
1751
|
|
|
|
1752
|
14 |
|
return static::create($str, $this->encoding); |
1753
|
|
|
} |
1754
|
|
|
|
1755
|
|
|
/** |
1756
|
|
|
* Splits the string with the provided regular expression, returning an |
1757
|
|
|
* array of Stringy objects. An optional integer $limit will truncate the |
1758
|
|
|
* results. |
1759
|
|
|
* |
1760
|
|
|
* @param string $pattern The regex with which to split the string |
1761
|
|
|
* @param int $limit Optional maximum number of results to return |
1762
|
|
|
* |
1763
|
|
|
* @return Stringy[] An array of Stringy objects |
1764
|
|
|
*/ |
1765
|
19 |
|
public function split($pattern, $limit = null) |
1766
|
|
|
{ |
1767
|
19 |
|
if ($limit === 0) { |
1768
|
2 |
|
return array(); |
1769
|
|
|
} |
1770
|
|
|
|
1771
|
|
|
// UTF8::split errors when supplied an empty pattern in < PHP 5.4.13 |
1772
|
|
|
// and current versions of HHVM (3.8 and below) |
1773
|
17 |
|
if ($pattern === '') { |
1774
|
1 |
|
return array(static::create($this->str, $this->encoding)); |
1775
|
|
|
} |
1776
|
|
|
|
1777
|
|
|
// UTF8::split returns the remaining unsplit string in the last index when |
1778
|
|
|
// supplying a limit |
1779
|
16 |
|
if ($limit > 0) { |
1780
|
8 |
|
$limit += 1; |
1781
|
|
|
} else { |
1782
|
8 |
|
$limit = -1; |
1783
|
|
|
} |
1784
|
|
|
|
1785
|
16 |
|
$array = preg_split('/' . preg_quote($pattern, '/') . '/u', $this->str, $limit); |
1786
|
|
|
|
1787
|
16 |
|
if ($limit > 0 && count($array) === $limit) { |
1788
|
4 |
|
array_pop($array); |
1789
|
|
|
} |
1790
|
|
|
|
1791
|
|
|
/** @noinspection CallableInLoopTerminationConditionInspection */ |
1792
|
|
|
/** @noinspection ForeachInvariantsInspection */ |
1793
|
16 |
View Code Duplication |
for ($i = 0; $i < count($array); $i++) { |
|
|
|
|
1794
|
16 |
|
$array[$i] = static::create($array[$i], $this->encoding); |
1795
|
|
|
} |
1796
|
|
|
|
1797
|
16 |
|
return $array; |
1798
|
|
|
} |
1799
|
|
|
|
1800
|
|
|
/** |
1801
|
|
|
* Surrounds $str with the given substring. |
1802
|
|
|
* |
1803
|
|
|
* @param string $substring The substring to add to both sides |
1804
|
|
|
* |
1805
|
|
|
* @return Stringy Object whose $str had the substring both prepended and |
1806
|
|
|
* appended |
1807
|
|
|
*/ |
1808
|
5 |
|
public function surround($substring) |
1809
|
|
|
{ |
1810
|
5 |
|
$str = implode('', array($substring, $this->str, $substring)); |
1811
|
|
|
|
1812
|
5 |
|
return static::create($str, $this->encoding); |
1813
|
|
|
} |
1814
|
|
|
|
1815
|
|
|
/** |
1816
|
|
|
* Returns a case swapped version of the string. |
1817
|
|
|
* |
1818
|
|
|
* @return Stringy Object whose $str has each character's case swapped |
1819
|
|
|
*/ |
1820
|
5 |
|
public function swapCase() |
1821
|
|
|
{ |
1822
|
5 |
|
$stringy = static::create($this->str, $this->encoding); |
1823
|
|
|
|
1824
|
5 |
|
$stringy->str = UTF8::swapCase($stringy->str, $stringy->encoding); |
1825
|
|
|
|
1826
|
5 |
|
return $stringy; |
1827
|
|
|
} |
1828
|
|
|
|
1829
|
|
|
/** |
1830
|
|
|
* Returns a string with smart quotes, ellipsis characters, and dashes from |
1831
|
|
|
* Windows-1252 (commonly used in Word documents) replaced by their ASCII |
1832
|
|
|
* equivalents. |
1833
|
|
|
* |
1834
|
|
|
* @return Stringy Object whose $str has those characters removed |
1835
|
|
|
*/ |
1836
|
4 |
|
public function tidy() |
1837
|
|
|
{ |
1838
|
4 |
|
$str = UTF8::normalize_msword($this->str); |
1839
|
|
|
|
1840
|
4 |
|
return static::create($str, $this->encoding); |
1841
|
|
|
} |
1842
|
|
|
|
1843
|
|
|
/** |
1844
|
|
|
* Returns a trimmed string with the first letter of each word capitalized. |
1845
|
|
|
* Also accepts an array, $ignore, allowing you to list words not to be |
1846
|
|
|
* capitalized. |
1847
|
|
|
* |
1848
|
|
|
* @param array $ignore An array of words not to capitalize |
1849
|
|
|
* |
1850
|
|
|
* @return Stringy Object with a titleized $str |
1851
|
|
|
*/ |
1852
|
5 |
|
public function titleize($ignore = null) |
1853
|
|
|
{ |
1854
|
5 |
|
$stringy = static::create($this->trim(), $this->encoding); |
1855
|
5 |
|
$encoding = $this->encoding; |
1856
|
|
|
|
1857
|
5 |
|
$stringy->str = preg_replace_callback( |
1858
|
5 |
|
'/([\S]+)/u', |
1859
|
|
|
function ($match) use ($encoding, $ignore) { |
1860
|
5 |
|
if ($ignore && in_array($match[0], $ignore, true)) { |
1861
|
2 |
|
return $match[0]; |
1862
|
|
|
} else { |
1863
|
5 |
|
$stringy = new Stringy($match[0], $encoding); |
1864
|
|
|
|
1865
|
5 |
|
return (string)$stringy->toLowerCase()->upperCaseFirst(); |
1866
|
|
|
} |
1867
|
5 |
|
}, |
1868
|
5 |
|
$stringy->str |
1869
|
|
|
); |
1870
|
|
|
|
1871
|
5 |
|
return $stringy; |
1872
|
|
|
} |
1873
|
|
|
|
1874
|
|
|
/** |
1875
|
|
|
* Converts all characters in the string to lowercase. An alias for PHP's |
1876
|
|
|
* UTF8::strtolower(). |
1877
|
|
|
* |
1878
|
|
|
* @return Stringy Object with all characters of $str being lowercase |
1879
|
|
|
*/ |
1880
|
27 |
|
public function toLowerCase() |
1881
|
|
|
{ |
1882
|
27 |
|
$str = UTF8::strtolower($this->str, $this->encoding); |
1883
|
|
|
|
1884
|
27 |
|
return static::create($str, $this->encoding); |
1885
|
|
|
} |
1886
|
|
|
|
1887
|
|
|
/** |
1888
|
|
|
* Returns true if the string is base64 encoded, false otherwise. |
1889
|
|
|
* |
1890
|
|
|
* @return bool Whether or not $str is base64 encoded |
1891
|
|
|
*/ |
1892
|
7 |
|
public function isBase64() |
1893
|
|
|
{ |
1894
|
7 |
|
return UTF8::is_base64($this->str); |
1895
|
|
|
} |
1896
|
|
|
|
1897
|
|
|
/** |
1898
|
|
|
* Returns an ASCII version of the string. A set of non-ASCII characters are |
1899
|
|
|
* replaced with their closest ASCII counterparts, and the rest are removed |
1900
|
|
|
* unless instructed otherwise. |
1901
|
|
|
* |
1902
|
|
|
* @param $strict [optional] <p>Use "transliterator_transliterate()" from PHP-Intl | WARNING: bad performance</p> |
1903
|
|
|
* |
1904
|
|
|
* @return Stringy Object whose $str contains only ASCII characters |
1905
|
|
|
*/ |
1906
|
16 |
|
public function toAscii($strict = false) |
1907
|
|
|
{ |
1908
|
16 |
|
$str = UTF8::to_ascii($this->str, '?', $strict); |
1909
|
|
|
|
1910
|
16 |
|
return static::create($str, $this->encoding); |
1911
|
|
|
} |
1912
|
|
|
|
1913
|
|
|
/** |
1914
|
|
|
* Returns a boolean representation of the given logical string value. |
1915
|
|
|
* For example, 'true', '1', 'on' and 'yes' will return true. 'false', '0', |
1916
|
|
|
* 'off', and 'no' will return false. In all instances, case is ignored. |
1917
|
|
|
* For other numeric strings, their sign will determine the return value. |
1918
|
|
|
* In addition, blank strings consisting of only whitespace will return |
1919
|
|
|
* false. For all other strings, the return value is a result of a |
1920
|
|
|
* boolean cast. |
1921
|
|
|
* |
1922
|
|
|
* @return bool A boolean value for the string |
1923
|
|
|
*/ |
1924
|
15 |
|
public function toBoolean() |
1925
|
|
|
{ |
1926
|
15 |
|
$key = $this->toLowerCase()->str; |
1927
|
|
|
$map = array( |
1928
|
15 |
|
'true' => true, |
1929
|
|
|
'1' => true, |
1930
|
|
|
'on' => true, |
1931
|
|
|
'yes' => true, |
1932
|
|
|
'false' => false, |
1933
|
|
|
'0' => false, |
1934
|
|
|
'off' => false, |
1935
|
|
|
'no' => false, |
1936
|
|
|
); |
1937
|
|
|
|
1938
|
15 |
|
if (array_key_exists($key, $map)) { |
1939
|
10 |
|
return $map[$key]; |
1940
|
5 |
|
} elseif (is_numeric($this->str)) { |
1941
|
2 |
|
return ((int)$this->str > 0); |
1942
|
|
|
} else { |
1943
|
3 |
|
return (bool)$this->regexReplace('[[:space:]]', '')->str; |
1944
|
|
|
} |
1945
|
|
|
} |
1946
|
|
|
|
1947
|
|
|
/** |
1948
|
|
|
* Return Stringy object as string, but you can also use (string) for automatically casting the object into a string. |
1949
|
|
|
* |
1950
|
|
|
* @return string |
1951
|
|
|
*/ |
1952
|
980 |
|
public function toString() |
1953
|
|
|
{ |
1954
|
980 |
|
return (string)$this->str; |
1955
|
|
|
} |
1956
|
|
|
|
1957
|
|
|
/** |
1958
|
|
|
* Converts each tab in the string to some number of spaces, as defined by |
1959
|
|
|
* $tabLength. By default, each tab is converted to 4 consecutive spaces. |
1960
|
|
|
* |
1961
|
|
|
* @param int $tabLength Number of spaces to replace each tab with |
1962
|
|
|
* |
1963
|
|
|
* @return Stringy Object whose $str has had tabs switched to spaces |
1964
|
|
|
*/ |
1965
|
6 |
View Code Duplication |
public function toSpaces($tabLength = 4) |
|
|
|
|
1966
|
|
|
{ |
1967
|
6 |
|
$spaces = UTF8::str_repeat(' ', $tabLength); |
1968
|
6 |
|
$str = UTF8::str_replace("\t", $spaces, $this->str); |
1969
|
|
|
|
1970
|
6 |
|
return static::create($str, $this->encoding); |
1971
|
|
|
} |
1972
|
|
|
|
1973
|
|
|
/** |
1974
|
|
|
* Converts each occurrence of some consecutive number of spaces, as |
1975
|
|
|
* defined by $tabLength, to a tab. By default, each 4 consecutive spaces |
1976
|
|
|
* are converted to a tab. |
1977
|
|
|
* |
1978
|
|
|
* @param int $tabLength Number of spaces to replace with a tab |
1979
|
|
|
* |
1980
|
|
|
* @return Stringy Object whose $str has had spaces switched to tabs |
1981
|
|
|
*/ |
1982
|
5 |
View Code Duplication |
public function toTabs($tabLength = 4) |
|
|
|
|
1983
|
|
|
{ |
1984
|
5 |
|
$spaces = UTF8::str_repeat(' ', $tabLength); |
1985
|
5 |
|
$str = UTF8::str_replace($spaces, "\t", $this->str); |
1986
|
|
|
|
1987
|
5 |
|
return static::create($str, $this->encoding); |
1988
|
|
|
} |
1989
|
|
|
|
1990
|
|
|
/** |
1991
|
|
|
* Converts the first character of each word in the string to uppercase. |
1992
|
|
|
* |
1993
|
|
|
* @return Stringy Object with all characters of $str being title-cased |
1994
|
|
|
*/ |
1995
|
5 |
|
public function toTitleCase() |
1996
|
|
|
{ |
1997
|
|
|
// "mb_convert_case()" used a polyfill from the "UTF8"-Class |
1998
|
5 |
|
$str = mb_convert_case($this->str, MB_CASE_TITLE, $this->encoding); |
1999
|
|
|
|
2000
|
5 |
|
return static::create($str, $this->encoding); |
2001
|
|
|
} |
2002
|
|
|
|
2003
|
|
|
/** |
2004
|
|
|
* Converts all characters in the string to uppercase. An alias for PHP's |
2005
|
|
|
* UTF8::strtoupper(). |
2006
|
|
|
* |
2007
|
|
|
* @return Stringy Object with all characters of $str being uppercase |
2008
|
|
|
*/ |
2009
|
5 |
|
public function toUpperCase() |
2010
|
|
|
{ |
2011
|
5 |
|
$str = UTF8::strtoupper($this->str, $this->encoding); |
2012
|
|
|
|
2013
|
5 |
|
return static::create($str, $this->encoding); |
2014
|
|
|
} |
2015
|
|
|
|
2016
|
|
|
/** |
2017
|
|
|
* Returns a string with whitespace removed from the start of the string. |
2018
|
|
|
* Supports the removal of unicode whitespace. Accepts an optional |
2019
|
|
|
* string of characters to strip instead of the defaults. |
2020
|
|
|
* |
2021
|
|
|
* @param string $chars Optional string of characters to strip |
2022
|
|
|
* |
2023
|
|
|
* @return Stringy Object with a trimmed $str |
2024
|
|
|
*/ |
2025
|
13 |
View Code Duplication |
public function trimLeft($chars = null) |
|
|
|
|
2026
|
|
|
{ |
2027
|
13 |
|
if (!$chars) { |
|
|
|
|
2028
|
11 |
|
$chars = '[:space:]'; |
2029
|
|
|
} else { |
2030
|
2 |
|
$chars = preg_quote($chars, '/'); |
2031
|
|
|
} |
2032
|
|
|
|
2033
|
13 |
|
return $this->regexReplace("^[$chars]+", ''); |
2034
|
|
|
} |
2035
|
|
|
|
2036
|
|
|
/** |
2037
|
|
|
* Returns a string with whitespace removed from the end of the string. |
2038
|
|
|
* Supports the removal of unicode whitespace. Accepts an optional |
2039
|
|
|
* string of characters to strip instead of the defaults. |
2040
|
|
|
* |
2041
|
|
|
* @param string $chars Optional string of characters to strip |
2042
|
|
|
* |
2043
|
|
|
* @return Stringy Object with a trimmed $str |
2044
|
|
|
*/ |
2045
|
13 |
View Code Duplication |
public function trimRight($chars = null) |
|
|
|
|
2046
|
|
|
{ |
2047
|
13 |
|
if (!$chars) { |
|
|
|
|
2048
|
11 |
|
$chars = '[:space:]'; |
2049
|
|
|
} else { |
2050
|
2 |
|
$chars = preg_quote($chars, '/'); |
2051
|
|
|
} |
2052
|
|
|
|
2053
|
13 |
|
return $this->regexReplace("[$chars]+\$", ''); |
2054
|
|
|
} |
2055
|
|
|
|
2056
|
|
|
/** |
2057
|
|
|
* Truncates the string to a given length. If $substring is provided, and |
2058
|
|
|
* truncating occurs, the string is further truncated so that the substring |
2059
|
|
|
* may be appended without exceeding the desired length. |
2060
|
|
|
* |
2061
|
|
|
* @param int $length Desired length of the truncated string |
2062
|
|
|
* @param string $substring The substring to append if it can fit |
2063
|
|
|
* |
2064
|
|
|
* @return Stringy Object with the resulting $str after truncating |
2065
|
|
|
*/ |
2066
|
22 |
View Code Duplication |
public function truncate($length, $substring = '') |
|
|
|
|
2067
|
|
|
{ |
2068
|
22 |
|
$stringy = static::create($this->str, $this->encoding); |
2069
|
22 |
|
if ($length >= $stringy->length()) { |
2070
|
4 |
|
return $stringy; |
2071
|
|
|
} |
2072
|
|
|
|
2073
|
|
|
// Need to further trim the string so we can append the substring |
2074
|
18 |
|
$substringLength = UTF8::strlen($substring, $stringy->encoding); |
2075
|
18 |
|
$length -= $substringLength; |
2076
|
|
|
|
2077
|
18 |
|
$truncated = UTF8::substr($stringy->str, 0, $length, $stringy->encoding); |
2078
|
18 |
|
$stringy->str = $truncated . $substring; |
2079
|
|
|
|
2080
|
18 |
|
return $stringy; |
2081
|
|
|
} |
2082
|
|
|
|
2083
|
|
|
/** |
2084
|
|
|
* Returns a lowercase and trimmed string separated by underscores. |
2085
|
|
|
* Underscores are inserted before uppercase characters (with the exception |
2086
|
|
|
* of the first character of the string), and in place of spaces as well as |
2087
|
|
|
* dashes. |
2088
|
|
|
* |
2089
|
|
|
* @return Stringy Object with an underscored $str |
2090
|
|
|
*/ |
2091
|
16 |
|
public function underscored() |
2092
|
|
|
{ |
2093
|
16 |
|
return $this->delimit('_'); |
2094
|
|
|
} |
2095
|
|
|
|
2096
|
|
|
/** |
2097
|
|
|
* Returns an UpperCamelCase version of the supplied string. It trims |
2098
|
|
|
* surrounding spaces, capitalizes letters following digits, spaces, dashes |
2099
|
|
|
* and underscores, and removes spaces, dashes, underscores. |
2100
|
|
|
* |
2101
|
|
|
* @return Stringy Object with $str in UpperCamelCase |
2102
|
|
|
*/ |
2103
|
13 |
|
public function upperCamelize() |
2104
|
|
|
{ |
2105
|
13 |
|
return $this->camelize()->upperCaseFirst(); |
2106
|
|
|
} |
2107
|
|
|
|
2108
|
|
|
/** |
2109
|
|
|
* Returns a camelCase version of the string. Trims surrounding spaces, |
2110
|
|
|
* capitalizes letters following digits, spaces, dashes and underscores, |
2111
|
|
|
* and removes spaces, dashes, as well as underscores. |
2112
|
|
|
* |
2113
|
|
|
* @return Stringy Object with $str in camelCase |
2114
|
|
|
*/ |
2115
|
32 |
|
public function camelize() |
2116
|
|
|
{ |
2117
|
32 |
|
$encoding = $this->encoding; |
2118
|
32 |
|
$stringy = $this->trim()->lowerCaseFirst(); |
2119
|
32 |
|
$stringy->str = preg_replace('/^[-_]+/', '', $stringy->str); |
2120
|
|
|
|
2121
|
32 |
|
$stringy->str = preg_replace_callback( |
2122
|
32 |
|
'/[-_\s]+(.)?/u', |
2123
|
|
|
function ($match) use ($encoding) { |
2124
|
27 |
|
if (isset($match[1])) { |
2125
|
27 |
|
return UTF8::strtoupper($match[1], $encoding); |
2126
|
|
|
} else { |
2127
|
1 |
|
return ''; |
2128
|
|
|
} |
2129
|
32 |
|
}, |
2130
|
32 |
|
$stringy->str |
2131
|
|
|
); |
2132
|
|
|
|
2133
|
32 |
|
$stringy->str = preg_replace_callback( |
2134
|
32 |
|
'/[\d]+(.)?/u', |
2135
|
|
|
function ($match) use ($encoding) { |
2136
|
6 |
|
return UTF8::strtoupper($match[0], $encoding); |
2137
|
32 |
|
}, |
2138
|
32 |
|
$stringy->str |
2139
|
|
|
); |
2140
|
|
|
|
2141
|
32 |
|
return $stringy; |
2142
|
|
|
} |
2143
|
|
|
|
2144
|
|
|
/** |
2145
|
|
|
* Convert a string to e.g.: "snake_case" |
2146
|
|
|
* |
2147
|
|
|
* @return Stringy Object with $str in snake_case |
2148
|
|
|
*/ |
2149
|
20 |
|
public function snakeize() |
2150
|
|
|
{ |
2151
|
20 |
|
$str = $this->str; |
2152
|
|
|
|
2153
|
20 |
|
$encoding = $this->encoding; |
2154
|
20 |
|
$str = UTF8::normalize_whitespace($str); |
2155
|
20 |
|
$str = str_replace('-', '_', $str); |
2156
|
|
|
|
2157
|
20 |
|
$str = preg_replace_callback( |
2158
|
20 |
|
'/([\d|A-Z])/u', |
2159
|
20 |
|
function ($matches) use ($encoding) { |
2160
|
8 |
|
$match = $matches[1]; |
2161
|
8 |
|
$matchInt = (int)$match; |
2162
|
|
|
|
2163
|
8 |
|
if ("$matchInt" == $match) { |
2164
|
4 |
|
return '_' . $match . '_'; |
2165
|
|
|
} else { |
2166
|
4 |
|
return '_' . UTF8::strtolower($match, $encoding); |
2167
|
|
|
} |
2168
|
20 |
|
}, |
2169
|
|
|
$str |
2170
|
|
|
); |
2171
|
|
|
|
2172
|
20 |
|
$str = preg_replace( |
2173
|
|
|
array( |
2174
|
|
|
|
2175
|
20 |
|
'/\s+/', // convert spaces to "_" |
2176
|
|
|
'/^\s+|\s+$/', // trim leading & trailing spaces |
2177
|
|
|
'/_+/', // remove double "_" |
2178
|
|
|
), |
2179
|
|
|
array( |
2180
|
20 |
|
'_', |
2181
|
|
|
'', |
2182
|
|
|
'_', |
2183
|
|
|
), |
2184
|
|
|
$str |
2185
|
|
|
); |
2186
|
|
|
|
2187
|
20 |
|
$str = UTF8::trim($str, '_'); // trim leading & trailing "_" |
2188
|
20 |
|
$str = UTF8::trim($str); // trim leading & trailing whitespace |
2189
|
|
|
|
2190
|
20 |
|
return static::create($str, $this->encoding); |
2191
|
|
|
} |
2192
|
|
|
|
2193
|
|
|
/** |
2194
|
|
|
* Converts the first character of the string to lower case. |
2195
|
|
|
* |
2196
|
|
|
* @return Stringy Object with the first character of $str being lower case |
2197
|
|
|
*/ |
2198
|
37 |
View Code Duplication |
public function lowerCaseFirst() |
|
|
|
|
2199
|
|
|
{ |
2200
|
37 |
|
$first = UTF8::substr($this->str, 0, 1, $this->encoding); |
2201
|
37 |
|
$rest = UTF8::substr($this->str, 1, $this->length() - 1, $this->encoding); |
2202
|
|
|
|
2203
|
37 |
|
$str = UTF8::strtolower($first, $this->encoding) . $rest; |
|
|
|
|
2204
|
|
|
|
2205
|
37 |
|
return static::create($str, $this->encoding); |
2206
|
|
|
} |
2207
|
|
|
|
2208
|
|
|
/** |
2209
|
|
|
* Shorten the string after $length, but also after the next word. |
2210
|
|
|
* |
2211
|
|
|
* @param int $length |
2212
|
|
|
* @param string $strAddOn |
2213
|
|
|
* |
2214
|
|
|
* @return Stringy |
2215
|
|
|
*/ |
2216
|
4 |
|
public function shortenAfterWord($length, $strAddOn = '...') |
2217
|
|
|
{ |
2218
|
4 |
|
$string = UTF8::str_limit_after_word($this->str, $length, $strAddOn); |
2219
|
|
|
|
2220
|
4 |
|
return static::create($string); |
2221
|
|
|
} |
2222
|
|
|
|
2223
|
|
|
/** |
2224
|
|
|
* Line-Wrap the string after $limit, but also after the next word. |
2225
|
|
|
* |
2226
|
|
|
* @param int $limit |
2227
|
|
|
* |
2228
|
|
|
* @return Stringy |
2229
|
|
|
*/ |
2230
|
1 |
|
public function lineWrapAfterWord($limit) |
2231
|
|
|
{ |
2232
|
1 |
|
$strings = preg_split('/\\r\\n|\\r|\\n/', $this->str); |
2233
|
|
|
|
2234
|
1 |
|
$string = ''; |
2235
|
1 |
|
foreach ($strings as $value) { |
2236
|
1 |
|
$string .= wordwrap($value, $limit); |
2237
|
1 |
|
$string .= "\n"; |
2238
|
|
|
} |
2239
|
|
|
|
2240
|
1 |
|
return static::create($string); |
2241
|
|
|
} |
2242
|
|
|
|
2243
|
|
|
/** |
2244
|
|
|
* Gets the substring after the first occurrence of a separator. |
2245
|
|
|
* If no match is found returns new empty Stringy object. |
2246
|
|
|
* |
2247
|
|
|
* @param string $separator |
2248
|
|
|
* |
2249
|
|
|
* @return Stringy |
2250
|
|
|
*/ |
2251
|
1 |
View Code Duplication |
public function afterFirst($separator) |
|
|
|
|
2252
|
|
|
{ |
2253
|
1 |
|
if (($offset = $this->indexOf($separator)) === false) { |
2254
|
1 |
|
return static::create(''); |
2255
|
|
|
} |
2256
|
|
|
|
2257
|
1 |
|
return static::create( |
2258
|
1 |
|
UTF8::substr( |
2259
|
1 |
|
$this->str, |
2260
|
1 |
|
$offset + UTF8::strlen($separator, $this->encoding), |
2261
|
1 |
|
null, |
2262
|
1 |
|
$this->encoding |
2263
|
|
|
), |
2264
|
1 |
|
$this->encoding |
2265
|
|
|
); |
2266
|
|
|
} |
2267
|
|
|
|
2268
|
|
|
/** |
2269
|
|
|
* Gets the substring after the last occurrence of a separator. |
2270
|
|
|
* If no match is found returns new empty Stringy object. |
2271
|
|
|
* |
2272
|
|
|
* @param string $separator |
2273
|
|
|
* |
2274
|
|
|
* @return Stringy |
2275
|
|
|
*/ |
2276
|
1 |
|
public function afterLast($separator) |
2277
|
|
|
{ |
2278
|
1 |
|
$offset = $this->indexOfLast($separator); |
2279
|
1 |
|
if ($offset === false) { |
2280
|
1 |
|
return static::create('', $this->encoding); |
2281
|
|
|
} |
2282
|
|
|
|
2283
|
1 |
|
return static::create( |
2284
|
1 |
|
UTF8::substr( |
2285
|
1 |
|
$this->str, |
2286
|
1 |
|
$offset + UTF8::strlen($separator, $this->encoding), |
2287
|
1 |
|
null, |
2288
|
1 |
|
$this->encoding |
2289
|
|
|
), |
2290
|
1 |
|
$this->encoding |
2291
|
|
|
); |
2292
|
|
|
} |
2293
|
|
|
|
2294
|
|
|
/** |
2295
|
|
|
* Gets the substring before the first occurrence of a separator. |
2296
|
|
|
* If no match is found returns new empty Stringy object. |
2297
|
|
|
* |
2298
|
|
|
* @param string $separator |
2299
|
|
|
* |
2300
|
|
|
* @return Stringy |
2301
|
|
|
*/ |
2302
|
1 |
View Code Duplication |
public function beforeFirst($separator) |
|
|
|
|
2303
|
|
|
{ |
2304
|
1 |
|
$offset = $this->indexOf($separator); |
2305
|
1 |
|
if ($offset === false) { |
2306
|
1 |
|
return static::create('', $this->encoding); |
2307
|
|
|
} |
2308
|
|
|
|
2309
|
1 |
|
return static::create( |
2310
|
1 |
|
UTF8::substr( |
2311
|
1 |
|
$this->str, |
2312
|
1 |
|
0, |
2313
|
|
|
$offset, |
2314
|
1 |
|
$this->encoding |
2315
|
|
|
), |
2316
|
1 |
|
$this->encoding |
2317
|
|
|
); |
2318
|
|
|
} |
2319
|
|
|
|
2320
|
|
|
/** |
2321
|
|
|
* Gets the substring before the last occurrence of a separator. |
2322
|
|
|
* If no match is found returns new empty Stringy object. |
2323
|
|
|
* |
2324
|
|
|
* @param string $separator |
2325
|
|
|
* |
2326
|
|
|
* @return Stringy |
2327
|
|
|
*/ |
2328
|
1 |
View Code Duplication |
public function beforeLast($separator) |
|
|
|
|
2329
|
|
|
{ |
2330
|
1 |
|
$offset = $this->indexOfLast($separator); |
2331
|
1 |
|
if ($offset === false) { |
2332
|
1 |
|
return static::create('', $this->encoding); |
2333
|
|
|
} |
2334
|
|
|
|
2335
|
1 |
|
return static::create( |
2336
|
1 |
|
UTF8::substr( |
2337
|
1 |
|
$this->str, |
2338
|
1 |
|
0, |
2339
|
|
|
$offset, |
2340
|
1 |
|
$this->encoding |
2341
|
|
|
), |
2342
|
1 |
|
$this->encoding |
2343
|
|
|
); |
2344
|
|
|
} |
2345
|
|
|
|
2346
|
|
|
/** |
2347
|
|
|
* Returns the string with the first letter of each word capitalized, |
2348
|
|
|
* except for when the word is a name which shouldn't be capitalized. |
2349
|
|
|
* |
2350
|
|
|
* @return Stringy Object with $str capitalized |
2351
|
|
|
*/ |
2352
|
39 |
|
public function capitalizePersonalName() |
2353
|
|
|
{ |
2354
|
39 |
|
$stringy = $this->collapseWhitespace(); |
2355
|
39 |
|
$stringy->str = $this->capitalizePersonalNameByDelimiter($stringy->str, ' '); |
|
|
|
|
2356
|
39 |
|
$stringy->str = $this->capitalizePersonalNameByDelimiter($stringy->str, '-'); |
|
|
|
|
2357
|
|
|
|
2358
|
39 |
|
return static::create($stringy, $this->encoding); |
2359
|
|
|
} |
2360
|
|
|
|
2361
|
|
|
/** |
2362
|
|
|
* @param string $word |
2363
|
|
|
* |
2364
|
|
|
* @return string |
2365
|
|
|
*/ |
2366
|
7 |
|
private function capitalizeWord($word) |
2367
|
|
|
{ |
2368
|
7 |
|
$encoding = $this->encoding; |
2369
|
|
|
|
2370
|
7 |
|
$firstCharacter = UTF8::substr($word, 0, 1, $encoding); |
2371
|
7 |
|
$restOfWord = UTF8::substr($word, 1, null, $encoding); |
2372
|
7 |
|
$firstCharacterUppercased = UTF8::strtoupper($firstCharacter, $encoding); |
|
|
|
|
2373
|
|
|
|
2374
|
7 |
|
return new static($firstCharacterUppercased . $restOfWord, $encoding); |
2375
|
|
|
} |
2376
|
|
|
|
2377
|
|
|
/** |
2378
|
|
|
* Personal names such as "Marcus Aurelius" are sometimes typed incorrectly using lowercase ("marcus aurelius"). |
2379
|
|
|
* |
2380
|
|
|
* @param string $names |
2381
|
|
|
* @param string $delimiter |
2382
|
|
|
* |
2383
|
|
|
* @return string |
2384
|
|
|
*/ |
2385
|
39 |
|
private function capitalizePersonalNameByDelimiter($names, $delimiter) |
2386
|
|
|
{ |
2387
|
|
|
// init |
2388
|
39 |
|
$names = explode($delimiter, $names); |
2389
|
39 |
|
$encoding = $this->encoding; |
2390
|
|
|
|
2391
|
|
|
$specialCases = array( |
2392
|
|
|
'names' => array( |
2393
|
|
|
'ab', |
2394
|
|
|
'af', |
2395
|
|
|
'al', |
2396
|
|
|
'and', |
2397
|
|
|
'ap', |
2398
|
|
|
'bint', |
2399
|
|
|
'binte', |
2400
|
|
|
'da', |
2401
|
|
|
'de', |
2402
|
|
|
'del', |
2403
|
|
|
'den', |
2404
|
|
|
'der', |
2405
|
|
|
'di', |
2406
|
|
|
'dit', |
2407
|
|
|
'ibn', |
2408
|
|
|
'la', |
2409
|
|
|
'mac', |
2410
|
|
|
'nic', |
2411
|
|
|
'of', |
2412
|
|
|
'ter', |
2413
|
|
|
'the', |
2414
|
|
|
'und', |
2415
|
|
|
'van', |
2416
|
|
|
'von', |
2417
|
|
|
'y', |
2418
|
|
|
'zu', |
2419
|
39 |
|
), |
2420
|
|
|
'prefixes' => array( |
2421
|
|
|
'al-', |
2422
|
|
|
"d'", |
2423
|
|
|
'ff', |
2424
|
|
|
"l'", |
2425
|
|
|
'mac', |
2426
|
|
|
'mc', |
2427
|
|
|
'nic', |
2428
|
|
|
), |
2429
|
|
|
); |
2430
|
|
|
|
2431
|
39 |
|
foreach ($names as &$name) { |
2432
|
39 |
|
if (in_array($name, $specialCases['names'], true)) { |
2433
|
27 |
|
continue; |
2434
|
|
|
} |
2435
|
|
|
|
2436
|
13 |
|
$continue = false; |
2437
|
|
|
|
2438
|
13 |
|
if ($delimiter == '-') { |
2439
|
13 |
View Code Duplication |
foreach ($specialCases['names'] as $beginning) { |
|
|
|
|
2440
|
13 |
|
if (UTF8::strpos($name, $beginning, null, $encoding) === 0) { |
2441
|
13 |
|
$continue = true; |
2442
|
|
|
} |
2443
|
|
|
} |
2444
|
|
|
} |
2445
|
|
|
|
2446
|
13 |
View Code Duplication |
foreach ($specialCases['prefixes'] as $beginning) { |
|
|
|
|
2447
|
13 |
|
if (UTF8::strpos($name, $beginning, null, $encoding) === 0) { |
2448
|
13 |
|
$continue = true; |
2449
|
|
|
} |
2450
|
|
|
} |
2451
|
|
|
|
2452
|
13 |
|
if ($continue) { |
2453
|
7 |
|
continue; |
2454
|
|
|
} |
2455
|
|
|
|
2456
|
7 |
|
$name = $this->capitalizeWord($name); |
2457
|
|
|
} |
2458
|
|
|
|
2459
|
39 |
|
return new static(implode($delimiter, $names), $encoding); |
2460
|
|
|
} |
2461
|
|
|
} |
2462
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: