Skip to content

Commit f21506e

Browse files
[8.x] Add a string helper to swap multiple keywords in a string. (#40831)
* Add a string helper to swap multiple keywords in a string. * styleci fixes. * styleci fixes. * Update Str.php * add to stringable Co-authored-by: Taylor Otwell <[email protected]>
1 parent 2eeb4f1 commit f21506e

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

src/Illuminate/Support/Str.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,18 @@ public static function substrReplace($string, $replace, $offset = 0, $length = n
908908
return substr_replace($string, $replace, $offset, $length);
909909
}
910910

911+
/**
912+
* Swap multiple keywords in a string with other keywords.
913+
*
914+
* @param array $map
915+
* @param string $subject
916+
* @return string
917+
*/
918+
public static function swap(array $map, $subject)
919+
{
920+
return str_replace(array_keys($map), array_values($map), $subject);
921+
}
922+
911923
/**
912924
* Make a string's first character uppercase.
913925
*

src/Illuminate/Support/Stringable.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,13 +726,24 @@ public function substrCount($needle, $offset = null, $length = null)
726726
* @param string|array $replace
727727
* @param array|int $offset
728728
* @param array|int|null $length
729-
* @return string|array
729+
* @return static
730730
*/
731731
public function substrReplace($replace, $offset = 0, $length = null)
732732
{
733733
return new static(Str::substrReplace($this->value, $replace, $offset, $length));
734734
}
735735

736+
/**
737+
* Swap multiple keywords in a string with other keywords.
738+
*
739+
* @param array $map
740+
* @return static
741+
*/
742+
public function swap(array $map)
743+
{
744+
return new static(str_replace(array_keys($map), array_values($map), $this->value));
745+
}
746+
736747
/**
737748
* Trim the string of the given characters.
738749
*

tests/Support/SupportStrTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,24 @@ public function testPadRight()
623623
$this->assertSame('Alien ', Str::padRight('Alien', 10));
624624
}
625625

626+
public function testSwapKeywords(): void
627+
{
628+
$this->assertSame(
629+
'PHP 8 is fantastic',
630+
Str::swap([
631+
'PHP' => 'PHP 8',
632+
'awesome' => 'fantastic',
633+
], 'PHP is awesome')
634+
);
635+
636+
$this->assertSame(
637+
'foo bar baz',
638+
Str::swap([
639+
'ⓐⓑ' => 'baz',
640+
], 'foo bar ⓐⓑ')
641+
);
642+
}
643+
626644
public function testWordCount()
627645
{
628646
$this->assertEquals(2, Str::wordCount('Hello, world!'));

tests/Support/SupportStringableTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,14 @@ public function testSubstr()
795795
$this->assertSame('', (string) $this->stringable('Б')->substr(2));
796796
}
797797

798+
public function testSwap()
799+
{
800+
$this->assertSame('PHP 8 is fantastic', (string) $this->stringable('PHP is awesome')->swap([
801+
'PHP' => 'PHP 8',
802+
'awesome' => 'fantastic',
803+
]));
804+
}
805+
798806
public function testSubstrCount()
799807
{
800808
$this->assertSame(3, $this->stringable('laravelPHPFramework')->substrCount('a'));

0 commit comments

Comments
 (0)