1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-11 07:44:12 +02:00

Add endsWith()

This commit is contained in:
Daniel St. Jules
2013-07-18 22:56:49 -04:00
parent e37cc8c72b
commit 493a194612
3 changed files with 70 additions and 5 deletions

View File

@@ -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**

View File

@@ -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;
}
}
?>

View File

@@ -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;
}
}
?>