From af1a88c95c34b45526b1c1ebc566a3ac7d13c741 Mon Sep 17 00:00:00 2001 From: Jason Lewis Date: Tue, 23 Oct 2012 15:35:40 +1100 Subject: [PATCH 1/2] Added the ends_with() helper. Signed-off-by: Jason Lewis --- src/helpers.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/helpers.php b/src/helpers.php index e8a376af3..de99d46c2 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -205,6 +205,18 @@ function starts_with($haystack, $needle) return strpos($haystack, $needle) === 0; } +/** + * Determine if a given string ends with a given needle. + * + * @param string $haystack + * @param string $needle + * @return bool + */ +function ends_with($haystack, $needle) +{ + return $needle == substr($haystack, strlen($haystack) - strlen($needle)); +} + /** * Determine if a given string contains a given sub-string. * From 86ee056538c22de7bd7f898693884793149973e8 Mon Sep 17 00:00:00 2001 From: Jason Lewis Date: Tue, 23 Oct 2012 19:17:15 +1100 Subject: [PATCH 2/2] Added starts_with() and ends_with() test cases. Signed-off-by: Jason Lewis --- tests/HelpersTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php index 9dad968dc..275a7673d 100644 --- a/tests/HelpersTest.php +++ b/tests/HelpersTest.php @@ -82,6 +82,20 @@ public function testStrIs() } + public function testStartsWith() + { + $this->assertTrue(starts_with('jason', 'jas')); + $this->assertFalse(starts_with('jason', 'day')); + } + + + public function testEndsWith() + { + $this->assertTrue(ends_with('jason', 'on')); + $this->assertFalse(ends_with('jason', 'no')); + } + + public function testStrContains() { $this->assertTrue(str_contains('taylor', 'ylo'));