From 029ed3716921b9546ad8aff3ee815db2cb69a9a3 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Wed, 18 Oct 2023 08:25:55 +0800 Subject: [PATCH 1/4] Added `Str::replaceMatches()` --- src/stringable/src/Str.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/stringable/src/Str.php b/src/stringable/src/Str.php index c62eb4e6e9..a4f8b3e4f5 100644 --- a/src/stringable/src/Str.php +++ b/src/stringable/src/Str.php @@ -11,6 +11,7 @@ */ namespace Hyperf\Stringable; +use Closure; use DateTimeInterface; use Hyperf\Collection\Arr; use Hyperf\Collection\Collection; @@ -1001,6 +1002,24 @@ public static function replaceEnd($search, $replace, $subject) return $subject; } + /** + * Replace the patterns matching the given regular expression. + * + * @param string $pattern + * @param Closure|string $replace + * @param array|string $subject + * @param int $limit + * @return null|string|string[] + */ + public static function replaceMatches($pattern, $replace, $subject, $limit = -1) + { + if ($replace instanceof Closure) { + return preg_replace_callback($pattern, $replace, $subject, $limit); + } + + return preg_replace($pattern, $replace, $subject, $limit); + } + public static function reverse($value): string { return implode(array_reverse(mb_str_split($value))); From 7c9dd6ad01cf965541b3e273ea5890493dbd0262 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Wed, 18 Oct 2023 08:26:41 +0800 Subject: [PATCH 2/4] Use in Stringable --- src/stringable/src/Stringable.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/stringable/src/Stringable.php b/src/stringable/src/Stringable.php index da8d91347f..bbe6345cf4 100644 --- a/src/stringable/src/Stringable.php +++ b/src/stringable/src/Stringable.php @@ -593,11 +593,7 @@ public function replaceLast($search, $replace) */ public function replaceMatches($pattern, $replace, $limit = -1) { - if ($replace instanceof Closure) { - return new static(preg_replace_callback($pattern, $replace, $this->value, $limit)); - } - - return new static(preg_replace($pattern, $replace, $this->value, $limit)); + return new static(Str::replaceMatches($pattern, $replace, $this->value, $limit)); } /** From 581aee82f9f373bd556cb60d8a6b9205b5154875 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Wed, 18 Oct 2023 08:26:52 +0800 Subject: [PATCH 3/4] Added test cases --- src/stringable/tests/StrTest.php | 6 ++++++ src/stringable/tests/StringableTest.php | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/stringable/tests/StrTest.php b/src/stringable/tests/StrTest.php index 1dea2edd98..218cdf98af 100644 --- a/src/stringable/tests/StrTest.php +++ b/src/stringable/tests/StrTest.php @@ -564,4 +564,10 @@ public function testReplaceLast() $this->assertSame('Hello earth', Str::replaceLast('world', 'earth', 'Hello world')); $this->assertSame('Hello world', Str::replaceLast('', 'earth', 'Hello world')); } + + public function testReplaceMatches() + { + $this->assertSame('http://hyperf.io', Str::replaceMatches('/^https:\/\//', 'http://', 'https://hyperf.io')); + $this->assertSame('http://hyperf.io', Str::replaceMatches('/^https:\/\//', fn ($matches) => 'http://', 'https://hyperf.io')); + } } diff --git a/src/stringable/tests/StringableTest.php b/src/stringable/tests/StringableTest.php index 567a88e882..da3c50e15a 100644 --- a/src/stringable/tests/StringableTest.php +++ b/src/stringable/tests/StringableTest.php @@ -456,6 +456,12 @@ public function testValueAndToString() $this->assertSame('foo', $this->stringable('foo')->toString()); } + public function testReplaceMatches() + { + $this->assertSame('http://hyperf.io', (string) $this->stringable('https://hyperf.io')->replaceMatches('/^https:\/\//', 'http://')); + $this->assertSame('http://hyperf.io', (string) $this->stringable('https://hyperf.io')->replaceMatches('/^https:\/\//', fn ($matches) => 'http://')); + } + /** * @param string $string * @return Stringable From 9341bf01c6df9b389d5ab564246eb0c94758a078 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Wed, 18 Oct 2023 08:28:21 +0800 Subject: [PATCH 4/4] Update CHANGELOG-3.0.md --- CHANGELOG-3.0.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG-3.0.md b/CHANGELOG-3.0.md index 62dcf4e96b..3c0c84a953 100644 --- a/CHANGELOG-3.0.md +++ b/CHANGELOG-3.0.md @@ -1,5 +1,9 @@ # v3.0.40 - TBD +## Added + +- [#6220](https://github.com/hyperf/hyperf/pull/6220) Added `Hyperf\Stringable\Str::replaceMatches()`. + ## Fixed - [#6217](https://github.com/hyperf/hyperf/pull/6217) Fixed bug that `Str::replaceLast` with empty search cannot work as expected.