From 38d44bb3214cf477a5f250605d2594fa9742f201 Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Thu, 10 Jun 2021 14:13:44 +0100 Subject: [PATCH] [8.x] Adds `match` and `matchAll` to `Str` (#37642) * Adds `match` and `matchAll` to `Str` * CS fix --- src/Illuminate/Support/Str.php | 36 +++++++++++++++++++++++++++ src/Illuminate/Support/Stringable.php | 16 ++---------- tests/Support/SupportStrTest.php | 12 +++++++++ 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 5bed2f5643ba..355998801b54 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -394,6 +394,42 @@ public static function markdown($string, array $options = []) return $converter->convertToHtml($string); } + /** + * Get the string matching the given pattern. + * + * @param string $pattern + * @param string $subject + * @return string + */ + public static function match($pattern, $subject) + { + preg_match($pattern, $subject, $matches); + + if (! $matches) { + return ''; + } + + return $matches[1] ?? $matches[0]; + } + + /** + * Get the string matching the given pattern. + * + * @param string $pattern + * @param string $subject + * @return \Illuminate\Support\Collection + */ + public static function matchAll($pattern, $subject) + { + preg_match_all($pattern, $subject, $matches); + + if (empty($matches[0])) { + return collect(); + } + + return collect($matches[1] ?? $matches[0]); + } + /** * Pad both sides of a string with another. * diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 7198670ff066..ccc958f3012b 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -340,13 +340,7 @@ public function markdown(array $options = []) */ public function match($pattern) { - preg_match($pattern, $this->value, $matches); - - if (! $matches) { - return new static; - } - - return new static($matches[1] ?? $matches[0]); + return new static(Str::match($pattern, $this->value)); } /** @@ -357,13 +351,7 @@ public function match($pattern) */ public function matchAll($pattern) { - preg_match_all($pattern, $this->value, $matches); - - if (empty($matches[0])) { - return collect(); - } - - return collect($matches[1] ?? $matches[0]); + return Str::matchAll($pattern, $this->value); } /** diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 249add1c6802..6d0801c1df63 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -428,6 +428,18 @@ public function testStudly() $this->assertSame('FooBarBaz', Str::studly('foo-bar_baz')); } + public function testMatch() + { + $this->assertSame('bar', Str::match('/bar/', 'foo bar')); + $this->assertSame('bar', Str::match('/foo (.*)/', 'foo bar')); + $this->assertEmpty(Str::match('/nothing/', 'foo bar')); + + $this->assertEquals(['bar', 'bar'], Str::matchAll('/bar/', 'bar foo bar')->all()); + + $this->assertEquals(['un', 'ly'], Str::matchAll('/f(\w*)/', 'bar fun bar fly')->all()); + $this->assertEmpty(Str::matchAll('/nothing/', 'bar fun bar fly')); + } + public function testCamel() { $this->assertSame('laravelPHPFramework', Str::camel('Laravel_p_h_p_framework'));