mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-12 00:04:11 +02:00
Add endsWith()
This commit is contained in:
19
README.md
19
README.md
@@ -21,6 +21,7 @@ A PHP library with a variety of string manipulation functions with multibyte sup
|
|||||||
* [padRight](#padright)
|
* [padRight](#padright)
|
||||||
* [padBoth](#padboth)
|
* [padBoth](#padboth)
|
||||||
* [startsWith](#startswith)
|
* [startsWith](#startswith)
|
||||||
|
* [endsWith](#endswith)
|
||||||
* [Tests](#tests)
|
* [Tests](#tests)
|
||||||
* [License](#license)
|
* [License](#license)
|
||||||
|
|
||||||
@@ -249,15 +250,23 @@ By default, the comparison is case-sensitive, but can be made insensitive
|
|||||||
by setting $caseSensitive to false.
|
by setting $caseSensitive to false.
|
||||||
|
|
||||||
```php
|
```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
|
## TODO
|
||||||
|
|
||||||
**endsWith**
|
|
||||||
|
|
||||||
**char** => chr
|
|
||||||
|
|
||||||
**toSpaces**
|
**toSpaces**
|
||||||
|
|
||||||
**toTabs**
|
**toTabs**
|
||||||
|
@@ -392,6 +392,35 @@ class Stringy {
|
|||||||
return $substring === $startOfStr;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
@@ -417,6 +417,33 @@ class StringyTestCase extends PHPUnit_Framework_TestCase {
|
|||||||
return $testData;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
Reference in New Issue
Block a user