-
-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathstrings.texy
More file actions
637 lines (428 loc) · 20 KB
/
strings.texy
File metadata and controls
637 lines (428 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
String Functions
****************
.[perex]
[api:Nette\Utils\Strings] is a static class containing useful functions for working with UTF-8 encoded strings.
Installation:
```shell
composer require nette/utils
```
All examples assume the following class alias is defined:
```php
use Nette\Utils\Strings;
```
Letter Case
===========
These functions require the PHP extension `mbstring`.
lower(string $s): string .[method]
----------------------------------
Converts a UTF-8 string to lowercase.
```php
Strings::lower('Hello World'); // 'hello world'
```
upper(string $s): string .[method]
----------------------------------
Converts a UTF-8 string to uppercase.
```php
Strings::upper('Hello World'); // 'HELLO WORLD'
```
firstUpper(string $s): string .[method]
---------------------------------------
Converts the first character of a UTF-8 string to uppercase and leaves the other characters unchanged.
```php
Strings::firstUpper('hello world'); // 'Hello world'
```
firstLower(string $s): string .[method]
---------------------------------------
Converts the first character of a UTF-8 string to lowercase and leaves the other characters unchanged.
```php
Strings::firstLower('Hello World'); // 'hello world'
```
capitalize(string $s): string .[method]
---------------------------------------
Converts the first character of every word in a UTF-8 string to uppercase and the others to lowercase.
```php
Strings::capitalize('hello world'); // 'Hello World'
```
Editing a String
================
normalize(string $s): string .[method]
--------------------------------------
Removes control characters, normalizes line endings to `\n`, trims leading and trailing blank lines, trims trailing spaces on lines, and normalizes UTF-8 to the NFC normal form.
unixNewLines(string $s): string .[method]
-----------------------------------------
Converts line endings to `\n` used on Unix systems. Line endings are: `\n`, `\r`, `\r\n`, U+2028 line separator, U+2029 paragraph separator.
```php
$unixLikeLines = Strings::unixNewLines($string);
```
platformNewLines(string $s): string .[method]
---------------------------------------------
Converts line endings to characters specific to the current platform, i.e., `\r\n` on Windows and `\n` elsewhere. Line endings are: `\n`, `\r`, `\r\n`, U+2028 line separator, U+2029 paragraph separator.
```php
$platformLines = Strings::platformNewLines($string);
```
webalize(string $s, ?string $charlist=null, bool $lower=true): string .[method]
-------------------------------------------------------------------------------
Modifies a UTF-8 string to the form used in URLs, i.e., removes diacritics and replaces all characters except letters of the English alphabet and numbers with hyphens.
```php
Strings::webalize('žluťoučký kůň'); // 'zlutoucky-kun'
```
If other characters should be preserved, they can be specified in the second parameter.
```php
Strings::webalize('10. image_id', '._'); // '10.-image_id'
```
The third parameter can suppress conversion to lowercase.
```php
Strings::webalize('Dobrý den', null, false); // 'Dobry-den'
```
.[caution]
Requires the PHP extension `intl`.
trim(string $s, ?string $charlist=null): string .[method]
---------------------------------------------------------
Strips leading and trailing whitespace (or other characters specified by the second parameter) from a UTF-8 string.
```php
Strings::trim(' Hello '); // 'Hello'
```
truncate(string $s, int $maxLen, string $append=`'…'`): string .[method]
------------------------------------------------------------------------
Truncates a UTF-8 string to the specified maximum length, trying to preserve whole words. If the string is truncated, an ellipsis (changeable via the third parameter) is added to the end.
```php
$text = 'Hello, how are you today?';
Strings::truncate($text, 5); // 'Hell…'
Strings::truncate($text, 20); // 'Hello, how are you…'
Strings::truncate($text, 30); // 'Hello, how are you today?'
Strings::truncate($text, 20, '~'); // 'Hello, how are you~'
```
indent(string $s, int $level=1, string $indentationChar=`"\t"`): string .[method]
---------------------------------------------------------------------------------
Indents a multiline text from the left. The second parameter specifies the number of indentation characters, and the third parameter specifies the character(s) to use for indentation (default is tab).
```php
Strings::indent('Nette'); // "\tNette"
Strings::indent('Nette', 2, '+'); // '++Nette'
```
padLeft(string $s, int $length, string $pad=`' '`): string .[method]
--------------------------------------------------------------------
Pads a UTF-8 string to a specified length by prepending the `$pad` string from the left.
```php
Strings::padLeft('Nette', 6); // ' Nette'
Strings::padLeft('Nette', 8, '+*'); // '+*+Nette'
```
padRight(string $s, int $length, string $pad=`' '`): string .[method]
---------------------------------------------------------------------
Pads a UTF-8 string to a specified length by appending the `$pad` string from the right.
```php
Strings::padRight('Nette', 6); // 'Nette '
Strings::padRight('Nette', 8, '+*'); // 'Nette+*+'
```
substring(string $s, int $start, ?int $length=null): string .[method]
---------------------------------------------------------------------
Returns a portion of a UTF-8 string `$s` specified by the starting position `$start` and length `$length`. If `$start` is negative, the returned string will start at the `$start`-th character from the end.
```php
Strings::substring('Nette Framework', 0, 5); // 'Nette'
Strings::substring('Nette Framework', 6); // 'Framework'
Strings::substring('Nette Framework', -4); // 'work'
```
reverse(string $s): string .[method]
------------------------------------
Reverses a UTF-8 string.
```php
Strings::reverse('Nette'); // 'etteN'
```
length(string $s): int .[method]
--------------------------------
Returns the number of characters (not bytes) in a UTF-8 string.
This is the number of Unicode code points, which may differ from the number of graphemes.
```php
Strings::length('Nette'); // 5
Strings::length('červená'); // 7
```
startsWith(string $haystack, string $needle): bool .[method deprecated]
-----------------------------------------------------------------------
Checks if the string `$haystack` starts with the string `$needle`.
```php
$haystack = 'Starts';
$needle = 'St';
Strings::startsWith($haystack, $needle); // true
```
.[note]
Use the native `str_starts_with()`:https://www.php.net/manual/en/function.str-starts-with.php function.
endsWith(string $haystack, string $needle): bool .[method deprecated]
---------------------------------------------------------------------
Checks if the string `$haystack` ends with the string `$needle`.
```php
$haystack = 'Ends';
$needle = 'ds';
Strings::endsWith($haystack, $needle); // true
```
.[note]
Use the native `str_ends_with()`:https://www.php.net/manual/en/function.str-ends-with.php function.
contains(string $haystack, string $needle): bool .[method deprecated]
---------------------------------------------------------------------
Checks if the string `$haystack` contains the string `$needle`.
```php
$haystack = 'Auditorium';
$needle = 'dit';
Strings::contains($haystack, $needle); // true
```
.[note]
Use the native `str_contains()`:https://www.php.net/manual/en/function.str-contains.php function.
compare(string $left, string $right, ?int $length=null): bool .[method]
-----------------------------------------------------------------------
Compares two UTF-8 strings or their parts, case-insensitively. If `$length` is null, compares the entire strings. If negative, compares the corresponding number of characters from the end of the strings. Otherwise, compares the corresponding number of characters from the beginning.
```php
Strings::compare('Nette', 'nette'); // true
Strings::compare('Nette', 'next', 2); // true - first 2 characters match
Strings::compare('Nette', 'Latte', -2); // true - last 2 characters match
```
findPrefix(...$strings): string .[method]
-----------------------------------------
Finds the common prefix of the strings. Returns an empty string if no common prefix is found.
```php
Strings::findPrefix('prefix-a', 'prefix-bb', 'prefix-c'); // 'prefix-'
Strings::findPrefix(['prefix-a', 'prefix-bb', 'prefix-c']); // 'prefix-'
Strings::findPrefix('Nette', 'is', 'great'); // ''
```
before(string $haystack, string $needle, int $nth=1): ?string .[method]
-----------------------------------------------------------------------
Returns the portion of string `$haystack` before the `$nth` occurrence of string `$needle`. Returns `null` if `$needle` is not found. If `$nth` is negative, it searches from the end of the string.
```php
Strings::before('Nette_is_great', '_', 1); // 'Nette'
Strings::before('Nette_is_great', '_', -2); // 'Nette'
Strings::before('Nette_is_great', ' '); // null
Strings::before('Nette_is_great', '_', 3); // null
```
after(string $haystack, string $needle, int $nth=1): ?string .[method]
----------------------------------------------------------------------
Returns the portion of string `$haystack` after the `$nth` occurrence of string `$needle`. Returns `null` if `$needle` is not found. If `$nth` is negative, it searches from the end of the string.
```php
Strings::after('Nette_is_great', '_', 2); // 'great'
Strings::after('Nette_is_great', '_', -1); // 'great'
Strings::after('Nette_is_great', ' '); // null
Strings::after('Nette_is_great', '_', 3); // null
```
indexOf(string $haystack, string $needle, int $nth=1): ?int .[method]
---------------------------------------------------------------------
Returns the character position of the `$nth` occurrence of string `$needle` in string `$haystack`. Returns `null` if `$needle` is not found. If `$nth` is negative, it searches from the end of the string.
```php
Strings::indexOf('abc abc abc', 'abc', 2); // 4
Strings::indexOf('abc abc abc', 'abc', -1); // 8
Strings::indexOf('abc abc abc', 'd'); // null
```
Encoding
========
fixEncoding(string $s): string .[method]
----------------------------------------
Removes invalid UTF-8 characters from a string.
```php
$correctString = Strings::fixEncoding($invalidString);
```
checkEncoding(string $s): bool .[method deprecated]
---------------------------------------------------
Checks if a string is a valid UTF-8 string.
```php
$isUtf8 = Strings::checkEncoding($string);
```
.[note]
Use [Nette\Utils\Validator::isUnicode() |validators#isUnicode].
toAscii(string $s): string .[method]
------------------------------------
Converts a UTF-8 string to ASCII, i.e., removes diacritics, etc.
```php
Strings::toAscii('žluťoučký kůň'); // 'zlutoucky kun'
```
.[caution]
Requires the PHP extension `intl`.
chr(int $code): string .[method]
--------------------------------
Returns a specific character in UTF-8 from a code point (a number in the range 0x0000..D7FF or 0xE000..10FFFF).
```php
Strings::chr(0xA9); // '©' in UTF-8 encoding
```
ord(string $char): int .[method]
--------------------------------
Returns the code point of a specific character in UTF-8 (a number in the range 0x0000..D7FF or 0xE000..10FFFF).
```php
Strings::ord('©'); // 169 (0xA9)
```
Regular Expressions
===================
The `Strings` class offers functions for working with regular expressions. Unlike native PHP functions, they feature a more understandable API, better Unicode support, and, crucially, error detection. Any error during compilation or expression processing throws a `Nette\RegexpException`.
split(string $subject, string $pattern, bool $captureOffset=false, bool $skipEmpty=false, int $limit=-1, bool $utf8=false): array .[method]
-------------------------------------------------------------------------------------------------------------------------------------------
Splits a string into an array using a regular expression. Expressions in parentheses will also be captured and returned.
```php
Strings::split('hello, world', '~,\s*~');
// ['hello', 'world']
Strings::split('hello, world', '~(,)\s*~');
// ['hello', ',', 'world']
```
If `$skipEmpty` is `true`, only non-empty items will be returned:
```php
Strings::split('hello, world, ', '~,\s*~');
// ['hello', 'world', '']
Strings::split('hello, world, ', '~,\s*~', skipEmpty: true);
// ['hello', 'world']
```
If `$limit` is specified, only substrings up to the limit are returned, and the rest of the string is placed in the last element. A limit of -1 or 0 means no limit.
```php
Strings::split('hello, world, third', '~,\s*~', limit: 2);
// ['hello', 'world, third']
```
If `$utf8` is `true`, evaluation switches to Unicode mode, similar to using the `u` modifier.
If `$captureOffset` is `true`, the position of each match in the string will also be returned (in bytes; in characters if `$utf8` is set). This changes the return value to an array where each element is a pair consisting of the matched string and its position.
```php
Strings::split('žlutý, kůň', '~,\s*~', captureOffset: true);
// [['žlutý', 0], ['kůň', 9]]
Strings::split('žlutý, kůň', '~,\s*~', captureOffset: true, utf8: true);
// [['žlutý', 0], ['kůň', 7]] // positions are in characters
```
match(string $subject, string $pattern, bool $captureOffset=false, int $offset=0, bool $unmatchedAsNull=false, bool $utf8=false): ?array .[method]
--------------------------------------------------------------------------------------------------------------------------------------------------
Searches a string for a part matching a regular expression and returns an array containing the found expression and individual subexpressions, or `null` if no match is found.
```php
Strings::match('hello!', '~\w+(!+)~');
// ['hello!', '!']
Strings::match('hello!', '~X~');
// null
```
If `$unmatchedAsNull` is `true`, unmatched subpatterns are returned as `null`; otherwise, they are returned as an empty string or omitted entirely:
```php
Strings::match('hello', '~\w+(!+)?~');
// ['hello'] (the optional group !+ did not match)
Strings::match('hello', '~\w+(!+)?~', unmatchedAsNull: true);
// ['hello', null]
```
If `$utf8` is `true`, evaluation switches to Unicode mode, similar to using the `u` modifier:
```php
Strings::match('žlutý kůň', '~\w+~'); // Without UTF-8
// ['lut'] (matches only ASCII word characters)
Strings::match('žlutý kůň', '~\w+~', utf8: true); // With UTF-8
// ['žlutý'] (matches Unicode word characters)
```
The `$offset` parameter can specify the starting position for the search (in bytes; in characters if `$utf8` is set).
If `$captureOffset` is `true`, the position of each match in the string will also be returned (in bytes; in characters if `$utf8` is set). This changes the return value to an array where each element is a pair consisting of the matched string and its offset:
```php
Strings::match('žlutý!', '~\w+(!+)?~', captureOffset: true); // Without UTF-8
// [['lut', 2]] (only ASCII match, offset in bytes)
Strings::match('žlutý!', '~\w+(!+)?~', captureOffset: true, utf8: true); // With UTF-8
// [['žlutý!', 0], ['!', 5]] (Unicode match, offsets in characters)
```
matchAll(string $subject, string $pattern, bool $captureOffset=false, int $offset=0, bool $unmatchedAsNull=false, bool $patternOrder=false, bool $utf8=false, bool $lazy=false): array|Generator .[method]
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Searches a string for all occurrences matching a regular expression and returns an array of arrays containing the found expression and individual subexpressions.
```php
Strings::matchAll('hello, world!!', '~\w+(!+)?~');
/* [
0 => ['hello'],
1 => ['world!!', '!!'],
] */
```
If `$patternOrder` is `true`, the structure of the results changes: the first element is an array of full pattern matches, the second is an array of strings matching the first parenthesized subpattern, and so on:
```php
Strings::matchAll('hello, world!!', '~\w+(!+)?~', patternOrder: true);
/* [
0 => ['hello', 'world!!'],
1 => ['', '!!'],
] */
```
If `$unmatchedAsNull` is `true`, unmatched subpatterns are returned as `null`; otherwise, they are returned as an empty string or omitted:
```php
Strings::matchAll('hello, world!!', '~\w+(!+)?~', unmatchedAsNull: true);
/* [
0 => ['hello', null],
1 => ['world!!', '!!'],
] */
```
If `$utf8` is `true`, evaluation switches to Unicode mode, similar to using the `u` modifier:
```php
Strings::matchAll('žlutý kůň', '~\w+~');
/* [
0 => ['lut'],
1 => ['k'],
] */
Strings::matchAll('žlutý kůň', '~\w+~', utf8: true);
/* [
0 => ['žlutý'],
1 => ['kůň'],
] */
```
The `$offset` parameter can specify the starting position for the search (in bytes; in characters if `$utf8` is set).
If `$captureOffset` is `true`, the position of each match in the string will also be returned (in bytes; in characters if `$utf8` is set). This changes the return value structure, where each match element is a pair `[matched_string, position]`:
```php
Strings::matchAll('žlutý kůň', '~\w+~', captureOffset: true);
/* [
0 => [['lut', 2]],
1 => [['k', 8]],
] */
Strings::matchAll('žlutý kůň', '~\w+~', captureOffset: true, utf8: true);
/* [
0 => [['žlutý', 0]],
1 => [['kůň', 6]],
] */
```
If `$lazy` is `true`, the function returns a `Generator` instead of an array. This offers significant performance benefits when working with large strings, as matches are found incrementally rather than processing the entire string at once. This allows efficient handling of extremely large inputs. Additionally, you can interrupt processing at any time if you find the desired match, saving computation time.
```php
$matches = Strings::matchAll($largeText, '~\w+~', lazy: true);
foreach ($matches as $match) {
echo "Found: $match[0]\n";
// Processing can be interrupted at any time, e.g., with break;
}
```
replace(string $subject, string|array $pattern, string|callable $replacement='', int $limit=-1, bool $captureOffset=false, bool $unmatchedAsNull=false, bool $utf8=false): string .[method]
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Replaces all occurrences matching a regular expression. `$replacement` is either a replacement string mask or a callback function.
```php
Strings::replace('hello, world!', '~\w+~', '--');
// '--, --!'
Strings::replace('hello, world!', '~\w+~', fn($m) => strrev($m[0]));
// 'olleh, dlrow!'
```
The function also allows multiple replacements by passing an array in the format `pattern => replacement` as the second parameter:
```php
Strings::replace('hello, world!', [
'~\w+~' => '--',
'~,\s+~' => ' ',
]);
// '-- --!'
```
The `$limit` parameter restricts the number of replacements performed. A limit of -1 means no limit.
If `$utf8` is `true`, evaluation switches to Unicode mode, similar to using the `u` modifier.
```php
Strings::replace('žlutý kůň', '~\w+~', '--');
// 'ž--ý --ůň'
Strings::replace('žlutý kůň', '~\w+~', '--', utf8: true);
// '-- --'
```
If `$captureOffset` is `true`, the position of each match in the string (in bytes; in characters if `$utf8` is set) is also passed to the callback. This changes the structure of the passed array, where each element is a pair `[matched_string, position]`.
```php
Strings::replace(
'žlutý kůň',
'~\w+~',
function (array $m) { dump($m); return ''; },
captureOffset: true,
);
// dumps [['lut', 2]] and [['k', 8]]
Strings::replace(
'žlutý kůň',
'~\w+~',
function (array $m) { dump($m); return ''; },
captureOffset: true,
utf8: true,
);
// dumps [['žlutý', 0]] and [['kůň', 6]]
```
If `$unmatchedAsNull` is `true`, unmatched subpatterns are passed to the callback as `null`; otherwise, they are passed as an empty string or omitted:
```php
Strings::replace(
'ac',
'~(a)(b)*(c)~',
function (array $m) { dump($m); return ''; },
);
// dumps ['ac', 'a', '', 'c']
Strings::replace(
'ac',
'~(a)(b)*(c)~',
function (array $m) { dump($m); return ''; },
unmatchedAsNull: true,
);
// dumps ['ac', 'a', null, 'c']
```