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

Add startsWith()

This commit is contained in:
Daniel St. Jules
2013-07-18 01:29:52 -04:00
parent 057f0310a7
commit 1311f69e10
3 changed files with 82 additions and 18 deletions

View File

@@ -195,7 +195,7 @@ S::standardize('fòô bàř'); // 'foo bar'
##### pad
S::pad(string $str , int $length [, string $padStr [, string $padType [, string $encoding]]])
S::pad(string $str , int $length [, string $padStr = ' ' [, string $padType = 'right' [, string $encoding ]]])
Pads a string to a given length with another string. If length is less
than or equal to the length of $str, then no padding takes places. The
@@ -208,7 +208,7 @@ S::pad('fòô bàř', 10, '¬ø', 'left', 'UTF-8'); // '¬ø¬fòô bàř'
##### padLeft
S::padLeft(string $str , int $length [, string $padStr [, string $encoding]])
S::padLeft(string $str , int $length [, string $padStr = ' ' [, string $encoding ]])
Returns a new string of a given length such that the beginning of the
string is padded. Alias for pad($str, $length, $padStr, 'left', $encoding)
@@ -219,7 +219,7 @@ S::padLeft('foo bar', 9, ' '); // ' foo bar'
##### padRight
S::padRight(string $str , int $length [, string $padStr [, string $encoding]])
S::padRight(string $str , int $length [, string $padStr = ' ' [, string $encoding ]])
Returns a new string of a given length such that the end of the string is
padded. Alias for pad($str, $length, $padStr, 'right', $encoding)
@@ -230,7 +230,7 @@ S::padRight('foo bar', 10, '_*'); // 'foo bar_*_'
##### padBoth
S::padBoth(string $str , int $length [, string $padStr [, string $encoding]])
S::padBoth(string $str , int $length [, string $padStr = ' ' [, string $encoding ]])
Returns a new string of a given length such that both sides of the string
string are padded. Alias for pad($str, $length, $padStr, 'both', $encoding)
@@ -239,14 +239,24 @@ string are padded. Alias for pad($str, $length, $padStr, 'both', $encoding)
S::padBoth('foo bar', 9, ' '); // ' foo bar '
```
##### startsWith
S::startsWith(string $str, string $substring [, boolean $caseSensitive = true [, string $encoding ]])
Returns true if the string $str begins with $substring, false otherwise.
By default, the comparison is case-sensitive, but can be made insensitive
by setting $caseSensitive to false.
```php
S::startsWith('foo bar', 'another', false, $encoding); // ' foo bar '
```
## TODO
**char** => chr
**startsWith**
**endsWith**
**char** => chr
**toSpaces**
**toTabs**

View File

@@ -365,6 +365,33 @@ class Stringy {
public static function padBoth($str, $length, $padStr = ' ', $encoding = null) {
return self::pad($str, $length, $padStr, 'both', $encoding);
}
/**
* Returns true if the string $str begins 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 start 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 starts with $substring
*/
public static function startsWith($str, $substring, $caseSensitive = true,
$encoding = null) {
$encoding = $encoding ?: mb_internal_encoding();
$substringLength = mb_strlen($substring, $encoding);
$startOfStr = mb_substr($str, 0, $substringLength, $encoding);
if (!$caseSensitive) {
$substring = mb_strtolower($substring, $encoding);
$startOfStr = mb_strtolower($startOfStr, $encoding);
}
return $substring === $startOfStr;
}
}
?>

View File

@@ -390,6 +390,33 @@ class StringyTestCase extends PHPUnit_Framework_TestCase {
return $testData;
}
/**
* @dataProvider stringsForStartsWith
*/
public function testStartsWith($expected, $string, $substring,
$caseSensitive = true, $encoding = null) {
$result = S::startsWith($string, $substring, $caseSensitive, $encoding);
$this->assertEquals($expected, $result);
}
public function stringsForStartsWith() {
$testData = array(
array(true, 'foo bars', 'foo bar'),
array(true, 'FOO bars', 'foo bar', false),
array(true, 'FOO bars', 'foo BAR', false),
array(true, 'FÒÔ bàřs', 'fòô bàř', false, 'UTF-8'),
array(true, 'fòô bàřs', 'fòô BÀŘ', false, 'UTF-8'),
array(false, 'foo bar', 'bar'),
array(false, 'foo bar', 'foo bars'),
array(false, 'FOO bar', 'foo bars'),
array(false, 'FOO bars', 'foo BAR'),
array(false, 'FÒÔ bàřs', 'fòô bàř', true, 'UTF-8'),
array(false, 'fòô bàřs', 'fòô BÀŘ', true, 'UTF-8'),
);
return $testData;
}
}
?>