Skip to content

Commit c040d67

Browse files
committed
add replaceArray() replaceFirst() replaceLast() start() startsWith() endsWith() finish() methods
1 parent cfa9827 commit c040d67

File tree

2 files changed

+191
-0
lines changed

2 files changed

+191
-0
lines changed

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ use Flextype\Component\Strings;
5858
| <a href="#strings_padBoth">`Strings::padBoth()`</a> | Pad both sides of a string with another. |
5959
| <a href="#strings_padLeft">`Strings::padLeft()`</a> | Pad the left side of a string with another. |
6060
| <a href="#strings_padRight">`Strings::padRight()`</a> | Pad the right side of a string with another. |
61+
| <a href="#strings_replaceArray">`Strings::replaceArray()`</a> | Replace a given value in the string sequentially with an array. |
62+
| <a href="#strings_replaceFirst">`Strings::replaceFirst()`</a> | Replace the first occurrence of a given value in the string. |
63+
| <a href="#strings_replaceLast">`Strings::replaceLast()`</a> | Replace the last occurrence of a given value in the string. |
64+
| <a href="#strings_start">`Strings::start()`</a> | Begin a string with a single instance of a given value. |
65+
| <a href="#strings_startsWith">`Strings::startsWith()`</a> | Determine if a given string starts with a given substring. |
66+
| <a href="#strings_endsWith">`Strings::endsWith()`</a> | Determine if a given string ends with a given substring. |
67+
| <a href="#strings_finish">`Strings::finish()`</a> | Cap a string with a single instance of a given value. |
6168

6269
<hr>
6370

@@ -389,6 +396,62 @@ Pad the left side of a string with another.
389396
$string = Strings::padLeft('SG-1 returns from an off-world mission', 50, '-');
390397
```
391398

399+
#### <a name="strings_replaceArray"></a> Method: `Strings::replaceArray()`
400+
401+
Replace a given value in the string sequentially with an array.
402+
403+
```php
404+
$string = Strings::replaceArray('SG-1 returns from an off-world mission', 'SG-1', ['SG-2']);
405+
```
406+
407+
#### <a name="strings_replaceFirst"></a> Method: `Strings::replaceFirst()`
408+
409+
Replace the first occurrence of a given value in the string.
410+
411+
```php
412+
$string = Strings::replaceFirst('SG-1 returns from an off-world mission', 'SG-1', 'SG-2');
413+
```
414+
415+
#### <a name="strings_replaceLast"></a> Method: `Strings::replaceLast()`
416+
417+
Replace the last occurrence of a given value in the string.
418+
419+
```php
420+
$string = Strings::replaceLast('SG-1 returns from an off-world mission', 'off-world', 'P9Y-3C3');
421+
```
422+
423+
#### <a name="strings_start"></a> Method: `Strings::start()`
424+
425+
Begin a string with a single instance of a given value.
426+
427+
```php
428+
$string = Strings::start('movies/sg-1/season-5/episode-21/', '/');
429+
```
430+
431+
#### <a name="strings_startsWith"></a> Method: `Strings::startsWith()`
432+
433+
Determine if a given string starts with a given substring.
434+
435+
```php
436+
$result = Strings::startsWith('/movies/sg-1/season-5/episode-21/', '/');
437+
```
438+
439+
#### <a name="strings_endsWith"></a> Method: `Strings::endsWith()`
440+
441+
Determine if a given string ends with a given substring.
442+
443+
```php
444+
$result = Strings::endsWith('/movies/sg-1/season-5/episode-21/', '/');
445+
```
446+
447+
#### <a name="strings_finish"></a> Method: `Strings::finish()`
448+
449+
Cap a string with a single instance of a given value.
450+
451+
```php
452+
$result = Strings::finish('/movies/sg-1/season-5/episode-21', '/');
453+
```
454+
392455
### License
393456
[The MIT License (MIT)](https://github.com/flextype-components/strings/blob/master/LICENSE.txt)
394457
Copyright (c) 2020 [Sergey Romanenko](https://github.com/Awilum)

src/Strings.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use function abs;
88
use function array_reverse;
9+
use function array_shift;
910
use function ctype_lower;
1011
use function explode;
1112
use function implode;
@@ -21,11 +22,18 @@
2122
use function mb_strwidth;
2223
use function mb_substr;
2324
use function preg_match;
25+
use function preg_quote;
2426
use function preg_replace;
2527
use function random_int;
2628
use function rtrim;
2729
use function str_pad;
2830
use function str_replace;
31+
use function strlen;
32+
use function strncmp;
33+
use function strpos;
34+
use function strrpos;
35+
use function substr;
36+
use function substr_replace;
2937
use function trim;
3038
use function ucwords;
3139

@@ -528,4 +536,124 @@ public static function stripSpaces(string $string): string
528536
{
529537
return preg_replace('/\s+/', '', $string);
530538
}
539+
540+
/**
541+
* Replace a given value in the string sequentially with an array.
542+
*
543+
* @param string $string String
544+
* @param string $search Search
545+
* @param array $replace Replace
546+
*/
547+
public static function replaceArray(string $string, string $search, array $replace): string
548+
{
549+
$segments = explode($search, $string);
550+
551+
$result = array_shift($segments);
552+
553+
foreach ($segments as $segment) {
554+
$result .= (array_shift($replace) ?? $search) . $segment;
555+
}
556+
557+
return $result;
558+
}
559+
560+
/**
561+
* Replace the first occurrence of a given value in the string.
562+
*
563+
* @param string $string String
564+
* @param string $search Search
565+
* @param string $replace Replace
566+
*/
567+
public static function replaceFirst(string $string, string $search, string $replace): string
568+
{
569+
if ($search === '') {
570+
return $string;
571+
}
572+
573+
$position = strpos($string, $search);
574+
575+
if ($position !== false) {
576+
return substr_replace($string, $replace, $position, strlen($search));
577+
}
578+
579+
return $search;
580+
}
581+
582+
/**
583+
* Replace the last occurrence of a given value in the string.
584+
*
585+
* @param string $string String
586+
* @param string $search Search
587+
* @param string $replace Replace
588+
*/
589+
public static function replaceLast(string $string, string $search, string $replace): string
590+
{
591+
$position = strrpos($string, $search);
592+
593+
if ($position !== false) {
594+
return substr_replace($string, $replace, $position, strlen($search));
595+
}
596+
597+
return $subject;
598+
}
599+
600+
/**
601+
* Begin a string with a single instance of a given value.
602+
*
603+
* @param string $string String
604+
* @param string $prefix Prefix
605+
*/
606+
public static function start(string $string, string $prefix): string
607+
{
608+
$quoted = preg_quote($prefix, '/');
609+
610+
return $prefix . preg_replace('/^(?:' . $quoted . ')+/u', '', $string);
611+
}
612+
613+
/**
614+
* Determine if a given string starts with a given substring.
615+
*
616+
* @param string $haystack Haystack
617+
* @param string|string[] $needles needles
618+
*/
619+
public static function startsWith(string $haystack, $needles): bool
620+
{
621+
foreach ((array) $needles as $needle) {
622+
if ((string) $needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0) {
623+
return true;
624+
}
625+
}
626+
627+
return false;
628+
}
629+
630+
/**
631+
* Determine if a given string ends with a given substring.
632+
*
633+
* @param string $haystack Haystack
634+
* @param string|string[] $needles needles
635+
*/
636+
public static function endsWith(string $haystack, $needles): bool
637+
{
638+
foreach ((array) $needles as $needle) {
639+
if ($needle !== '' && substr($haystack, -strlen($needle)) === (string) $needle) {
640+
return true;
641+
}
642+
}
643+
644+
return false;
645+
}
646+
647+
/**
648+
* Cap a string with a single instance of a given value.
649+
*
650+
* @param string $string String
651+
* @param string $cap Cap
652+
*/
653+
public static function finish(string $string, string $cap): string
654+
{
655+
$quoted = preg_quote($cap, '/');
656+
657+
return preg_replace('/(?:' . $quoted . ')+$/u', '', $string) . $cap;
658+
}
531659
}

0 commit comments

Comments
 (0)