From 9b8753a114d7b858461e44a01cb86a390fd8281c Mon Sep 17 00:00:00 2001 From: Caleb Porzio Date: Fri, 26 May 2017 10:42:29 -0400 Subject: [PATCH 1/3] Add str_after helper function --- src/Illuminate/Support/Str.php | 18 ++++++++++++++++++ src/Illuminate/Support/helpers.php | 14 ++++++++++++++ tests/Support/SupportHelpersTest.php | 7 +++++++ tests/Support/SupportStrTest.php | 7 +++++++ 4 files changed, 46 insertions(+) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 194d499a76a5..7e6878613210 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -29,6 +29,24 @@ class Str */ protected static $studlyCache = []; + /** + * Return the remainder of a string after a given value. + * + * @param string $subject + * @param string $search + * @return string + */ + public static function after($subject, $search) + { + if (! static::contains($subject, $search)) { + return ''; + } + + $searchEndPos = strpos($subject, $search) + static::length($search); + + return static::substr($subject, $searchEndPos, static::length($subject)); + } + /** * Transliterate a UTF-8 value to ASCII. * diff --git a/src/Illuminate/Support/helpers.php b/src/Illuminate/Support/helpers.php index dfa9a8fb0a99..48aa911531b9 100755 --- a/src/Illuminate/Support/helpers.php +++ b/src/Illuminate/Support/helpers.php @@ -695,6 +695,20 @@ function starts_with($haystack, $needles) } } +if (! function_exists('str_after')) { + /** + * Return the remainder of a string after a given value. + * + * @param string $subject + * @param string $search + * @return string + */ + function str_after($subject, $search) + { + return Str::after($subject, $search); + } +} + if (! function_exists('str_contains')) { /** * Determine if a given string contains a given substring. diff --git a/tests/Support/SupportHelpersTest.php b/tests/Support/SupportHelpersTest.php index 05f0f80a08ed..ef2079880e65 100755 --- a/tests/Support/SupportHelpersTest.php +++ b/tests/Support/SupportHelpersTest.php @@ -252,6 +252,13 @@ public function testEndsWith() $this->assertFalse(Str::endsWith('jason', ['no'])); } + public function testStrAfter() + { + $this->assertEquals('nah', str_after('hannah', 'han')); + $this->assertEquals('nah', str_after('hannah', 'n')); + $this->assertEmpty(str_after('hannah', 'caleb')); + } + public function testStrContains() { $this->assertTrue(Str::contains('taylor', 'ylo')); diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index b50259021099..d433d74c6d2d 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -76,6 +76,13 @@ public function testEndsWith() $this->assertFalse(Str::endsWith(0.27, '8')); } + public function testStrAfter() + { + $this->assertEquals('nah', Str::after('hannah', 'han')); + $this->assertEquals('nah', Str::after('hannah', 'n')); + $this->assertEmpty(Str::after('hannah', 'xxxx')); + } + public function testStrContains() { $this->assertTrue(Str::contains('taylor', 'ylo')); From 98b099e6f11974ff5f83a7918cec728437a01539 Mon Sep 17 00:00:00 2001 From: Caleb Porzio Date: Fri, 26 May 2017 10:49:13 -0400 Subject: [PATCH 2/3] Fix styling --- src/Illuminate/Support/Str.php | 2 +- tests/Support/SupportHelpersTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 7e6878613210..28295430c102 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -42,7 +42,7 @@ public static function after($subject, $search) return ''; } - $searchEndPos = strpos($subject, $search) + static::length($search); + $searchEndPos = strpos($subject, $search) + static::length($search); return static::substr($subject, $searchEndPos, static::length($subject)); } diff --git a/tests/Support/SupportHelpersTest.php b/tests/Support/SupportHelpersTest.php index ef2079880e65..2668045cc0ee 100755 --- a/tests/Support/SupportHelpersTest.php +++ b/tests/Support/SupportHelpersTest.php @@ -256,7 +256,7 @@ public function testStrAfter() { $this->assertEquals('nah', str_after('hannah', 'han')); $this->assertEquals('nah', str_after('hannah', 'n')); - $this->assertEmpty(str_after('hannah', 'caleb')); + $this->assertEmpty(str_after('hannah', 'xxxx')); } public function testStrContains() From 4c4810a4192d764cea26f150b49d2a1c828488dd Mon Sep 17 00:00:00 2001 From: Caleb Porzio Date: Sat, 27 May 2017 20:41:37 -0700 Subject: [PATCH 3/3] Return entire string if search is not found. --- src/Illuminate/Support/Str.php | 2 +- tests/Support/SupportHelpersTest.php | 2 +- tests/Support/SupportStrTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 28295430c102..d9ac85728d5c 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -39,7 +39,7 @@ class Str public static function after($subject, $search) { if (! static::contains($subject, $search)) { - return ''; + return $subject; } $searchEndPos = strpos($subject, $search) + static::length($search); diff --git a/tests/Support/SupportHelpersTest.php b/tests/Support/SupportHelpersTest.php index 2668045cc0ee..445d907a5a5f 100755 --- a/tests/Support/SupportHelpersTest.php +++ b/tests/Support/SupportHelpersTest.php @@ -256,7 +256,7 @@ public function testStrAfter() { $this->assertEquals('nah', str_after('hannah', 'han')); $this->assertEquals('nah', str_after('hannah', 'n')); - $this->assertEmpty(str_after('hannah', 'xxxx')); + $this->assertEquals('hannah', str_after('hannah', 'xxxx')); } public function testStrContains() diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index d433d74c6d2d..c3e84463cf18 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -80,7 +80,7 @@ public function testStrAfter() { $this->assertEquals('nah', Str::after('hannah', 'han')); $this->assertEquals('nah', Str::after('hannah', 'n')); - $this->assertEmpty(Str::after('hannah', 'xxxx')); + $this->assertEquals('hannah', Str::after('hannah', 'xxxx')); } public function testStrContains()