You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `Arr::reduceAllowedStr()` method builds a string of unique words from an array's keys based on their boolean values:
295
+
296
+
```php
297
+
use \TypeRocket\Utility\Arr;
298
+
299
+
$array = [
300
+
'button button-primary' => true,
301
+
'button' => true,
302
+
'link link-external' => false,
303
+
];
304
+
305
+
Arr::reduceAllowedStr($array);
306
+
// 'button button-primary'
307
+
```
308
+
292
309
### Arr::replaceRecursivePreferNew
293
310
294
311
The `Arr::replaceRecursivePreferNew()` method works like `array_replace_recursive` but keeps the order of the new array and allows for setting stop break points to the replacement search. The method take three arguments:
The `Str::classNames()` method constructs a list of classes for an HTML element. The method builds a string of unique words from an array's keys based on their boolean values:
386
+
387
+
```php
388
+
use \TypeRocket\Utility\Str;
389
+
390
+
$array = [
391
+
'button' => true,
392
+
'button-primary' => true,
393
+
'button-large' => true,
394
+
'button-disabled' => false,
395
+
];
396
+
397
+
Str::classNames($array);
398
+
// 'button button-primary button-large'
399
+
```
400
+
401
+
If the first argument is a string it will be appended to the result:
402
+
403
+
```php
404
+
use \TypeRocket\Utility\Str;
405
+
406
+
$base = 'button';
407
+
408
+
$array = [
409
+
'button-primary' => true,
410
+
'button-large' => true,
411
+
'button-disabled' => false,
412
+
];
413
+
414
+
Str::classNames($base, $array);
415
+
// 'button button-primary button-large'
416
+
```
417
+
418
+
Finally, if a third argument is provided, and all array values are false, it will be prepended to the base string as a fallback:
419
+
420
+
```php
421
+
use \TypeRocket\Utility\Str;
422
+
423
+
$base = 'button';
424
+
425
+
$array = [
426
+
'button-primary' => false,
427
+
'button-large' => false,
428
+
];
429
+
430
+
$fallback = 'button-disabled';
431
+
432
+
Str::classNames($base, $array, $fallback);
433
+
// 'button button-primary button-large'
434
+
```
435
+
366
436
### Str::contains
367
437
368
438
Use the method `Str::contains()` to test if a string contains a value.
@@ -416,9 +486,67 @@ Str::notBlank(' ');
416
486
// true
417
487
```
418
488
489
+
### Str::replaceFirst
490
+
491
+
The `Str::replaceFirst()` method replaces the first occurrence of a given value in the string:
492
+
493
+
```php
494
+
use \TypeRocket\Utility\Str;
495
+
496
+
$replace = 'mom';
497
+
$with = 'po';
498
+
$string = 'momster mom';
499
+
500
+
Str::replaceFirst($replace, $with, $string);
501
+
// 'poster mom'
502
+
```
503
+
504
+
### Str::replaceFirstRegex
505
+
506
+
The `Str::replaceFirstRegex()` method replaces only the first match in a given string that is escaped by default with `preg_quote()`:
507
+
508
+
```php
509
+
use \TypeRocket\Utility\Str;
510
+
511
+
$replace = 'mom';
512
+
$with = 'po';
513
+
$string = 'momster mom';
514
+
515
+
Str::replaceFirstRegex($replace, $with, $string);
516
+
// 'poster mom'
517
+
```
518
+
519
+
Further, you can disable regex escaping of the replacement argument to enable regex:
0 commit comments