Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Str::replaceMatches() #6220

Merged
merged 4 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG-3.0.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
19 changes: 19 additions & 0 deletions src/stringable/src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
namespace Hyperf\Stringable;

use Closure;
use DateTimeInterface;
use Hyperf\Collection\Arr;
use Hyperf\Collection\Collection;
Expand Down Expand Up @@ -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)));
Expand Down
6 changes: 1 addition & 5 deletions src/stringable/src/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/stringable/tests/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
6 changes: 6 additions & 0 deletions src/stringable/tests/StringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down