From 493a1946129612d7f6d470f0905f9d953fce1a5c Mon Sep 17 00:00:00 2001 From: "Daniel St. Jules" Date: Thu, 18 Jul 2013 22:56:49 -0400 Subject: [PATCH] Add endsWith() --- README.md | 19 ++++++++++++++----- src/Stringy/Stringy.php | 29 +++++++++++++++++++++++++++++ tests/Stringy/StringyTest.php | 27 +++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a591459..2275bdc 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ A PHP library with a variety of string manipulation functions with multibyte sup * [padRight](#padright) * [padBoth](#padboth) * [startsWith](#startswith) + * [endsWith](#endswith) * [Tests](#tests) * [License](#license) @@ -249,15 +250,23 @@ By default, the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false. ```php -S::startsWith('FÒÔ bàřs', 'fòô bàř', false, 'UTF-8'), // true +S::startsWith('FÒÔ bàřs', 'fòô bàř', false, 'UTF-8'); // true +``` + +##### endsWith + +S::endsWith(string $str, string $substring [, boolean $caseSensitive = true [, string $encoding ]]) + +Returns true if the string $str ends with $substring, false otherwise. +By default, the comparison is case-sensitive, but can be made insensitive +by setting $caseSensitive to false. + +```php +S::endsWith('FÒÔ bàřs', 'àřs', true, 'UTF-8'); // true ``` ## TODO -**endsWith** - -**char** => chr - **toSpaces** **toTabs** diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index 998b264..83a9d4f 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -392,6 +392,35 @@ class Stringy { return $substring === $startOfStr; } + /** + * Returns true if the string $str ends with $substring, false otherwise. + * By default, the comparison is case-sensitive, but can be made insensitive + * by setting $caseSensitive to false. + * + * @param string $str String to check the end of + * @param string $substring The substring to look for + * @param bool $caseSensitive Whether or not to enfore case-sensitivity + * @param string $encoding The character encoding + * @return bool Whether or not $str ends with $substring + */ + public static function endsWith($str, $substring, $caseSensitive = true, + $encoding = null) { + $encoding = $encoding ?: mb_internal_encoding(); + + $substringLength = mb_strlen($substring, $encoding); + $strLength = mb_strlen($str, $encoding); + + $endOfStr = mb_substr($str, $strLength - $substringLength, + $substringLength, $encoding); + + if (!$caseSensitive) { + $substring = mb_strtolower($substring, $encoding); + $endOfStr = mb_strtolower($endOfStr, $encoding); + } + + return $substring === $endOfStr; + } + } ?> diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index e16f576..ebf2348 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -417,6 +417,33 @@ class StringyTestCase extends PHPUnit_Framework_TestCase { return $testData; } + /** + * @dataProvider stringsForEndsWith + */ + public function testEndsWith($expected, $string, $substring, + $caseSensitive = true, $encoding = null) { + $result = S::endsWith($string, $substring, $caseSensitive, $encoding); + $this->assertEquals($expected, $result); + } + + public function stringsForEndsWith() { + $testData = array( + array(true, 'foo bars', 'o bars'), + array(true, 'FOO bars', 'o bars', false), + array(true, 'FOO bars', 'o BARs', false), + array(true, 'FÒÔ bàřs', 'ô bàřs', false, 'UTF-8'), + array(true, 'fòô bàřs', 'ô BÀŘs', false, 'UTF-8'), + array(false, 'foo bar', 'foo'), + array(false, 'foo bar', 'foo bars'), + array(false, 'FOO bar', 'foo bars'), + array(false, 'FOO bars', 'foo BARS'), + array(false, 'FÒÔ bàřs', 'fòô bàřs', true, 'UTF-8'), + array(false, 'fòô bàřs', 'fòô BÀŘS', true, 'UTF-8'), + ); + + return $testData; + } + } ?>