Skip to content

Commit 103f3a9

Browse files
authored
Added Str::replaceMatches() (#6220)
1 parent 4f1781c commit 103f3a9

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

src/Str.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
namespace Hyperf\Stringable;
1313

14+
use Closure;
1415
use DateTimeInterface;
1516
use Hyperf\Collection\Arr;
1617
use Hyperf\Collection\Collection;
@@ -1001,6 +1002,24 @@ public static function replaceEnd($search, $replace, $subject)
10011002
return $subject;
10021003
}
10031004

1005+
/**
1006+
* Replace the patterns matching the given regular expression.
1007+
*
1008+
* @param string $pattern
1009+
* @param Closure|string $replace
1010+
* @param array|string $subject
1011+
* @param int $limit
1012+
* @return null|string|string[]
1013+
*/
1014+
public static function replaceMatches($pattern, $replace, $subject, $limit = -1)
1015+
{
1016+
if ($replace instanceof Closure) {
1017+
return preg_replace_callback($pattern, $replace, $subject, $limit);
1018+
}
1019+
1020+
return preg_replace($pattern, $replace, $subject, $limit);
1021+
}
1022+
10041023
public static function reverse($value): string
10051024
{
10061025
return implode(array_reverse(mb_str_split($value)));

src/Stringable.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -593,11 +593,7 @@ public function replaceLast($search, $replace)
593593
*/
594594
public function replaceMatches($pattern, $replace, $limit = -1)
595595
{
596-
if ($replace instanceof Closure) {
597-
return new static(preg_replace_callback($pattern, $replace, $this->value, $limit));
598-
}
599-
600-
return new static(preg_replace($pattern, $replace, $this->value, $limit));
596+
return new static(Str::replaceMatches($pattern, $replace, $this->value, $limit));
601597
}
602598

603599
/**

tests/StrTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,4 +564,10 @@ public function testReplaceLast()
564564
$this->assertSame('Hello earth', Str::replaceLast('world', 'earth', 'Hello world'));
565565
$this->assertSame('Hello world', Str::replaceLast('', 'earth', 'Hello world'));
566566
}
567+
568+
public function testReplaceMatches()
569+
{
570+
$this->assertSame('http://hyperf.io', Str::replaceMatches('/^https:\/\//', 'http://', 'https://hyperf.io'));
571+
$this->assertSame('http://hyperf.io', Str::replaceMatches('/^https:\/\//', fn ($matches) => 'http://', 'https://hyperf.io'));
572+
}
567573
}

tests/StringableTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,12 @@ public function testValueAndToString()
456456
$this->assertSame('foo', $this->stringable('foo')->toString());
457457
}
458458

459+
public function testReplaceMatches()
460+
{
461+
$this->assertSame('http://hyperf.io', (string) $this->stringable('https://hyperf.io')->replaceMatches('/^https:\/\//', 'http://'));
462+
$this->assertSame('http://hyperf.io', (string) $this->stringable('https://hyperf.io')->replaceMatches('/^https:\/\//', fn ($matches) => 'http://'));
463+
}
464+
459465
/**
460466
* @param string $string
461467
* @return Stringable

0 commit comments

Comments
 (0)