mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-12 16:24:00 +02:00
Add reverse()
This commit is contained in:
19
README.md
19
README.md
@@ -29,6 +29,7 @@ A PHP library with a variety of string manipulation functions with multibyte sup
|
||||
* [surround](#surround)
|
||||
* [insert](#insert)
|
||||
* [truncate](#truncate)
|
||||
* [reverse](#reverse)
|
||||
* [Tests](#tests)
|
||||
* [License](#license)
|
||||
|
||||
@@ -340,7 +341,7 @@ S::insert('fòô bà', 'ř', 6, 'UTF-8'); // 'fòô bàř'
|
||||
|
||||
##### truncate
|
||||
|
||||
S::truncate(string $str, int $length, [, string $substring = '' [, string $encoding ] ])
|
||||
S::truncate(string $str, int $length, [, string $substring = '' [, string $encoding ]])
|
||||
|
||||
Truncates the string to a given length, while ensuring that it does not
|
||||
chop words. If $substring is provided, and truncating occurs, the string
|
||||
@@ -351,12 +352,18 @@ exceeding the desired length.
|
||||
S::truncate('What are your plans today?', 22, '...'); // 'What are your plans...'
|
||||
```
|
||||
|
||||
##### reverse
|
||||
|
||||
S::reverse(string $str, [, string $encoding ])
|
||||
|
||||
Reverses a string. A multibyte version of strrev.
|
||||
|
||||
```php
|
||||
S::reverse('', 'UTF-8'); //
|
||||
```
|
||||
|
||||
## TODO
|
||||
|
||||
**wordWrap** => wordwrap
|
||||
|
||||
**reverse** => strrev
|
||||
|
||||
**shuffle** => str_shuffle
|
||||
|
||||
**explode** => explode
|
||||
@@ -369,8 +376,6 @@ S::truncate('What are your plans today?', 22, '...'); // 'What are your plans...
|
||||
|
||||
**wordCount** => str_word_count
|
||||
|
||||
**isJson**
|
||||
|
||||
**isMultibyte**
|
||||
|
||||
## Tests
|
||||
|
@@ -550,6 +550,27 @@ class Stringy {
|
||||
return $truncated . $substring;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses a string. A multibyte version of strrev.
|
||||
*
|
||||
* @param string $str String to reverse
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The reversed string
|
||||
*/
|
||||
public static function reverse($str, $encoding = null) {
|
||||
$encoding = $encoding ?: mb_internal_encoding();
|
||||
|
||||
$strLength = mb_strlen($str, $encoding);
|
||||
$reversed = '';
|
||||
|
||||
// Loop from last index of string to first
|
||||
for ($i = $strLength - 1; $i >= 0; $i--) {
|
||||
$reversed .= mb_substr($str, $i, 1, $encoding);
|
||||
}
|
||||
|
||||
return $reversed;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
@@ -612,6 +612,25 @@ class StringyTestCase extends PHPUnit_Framework_TestCase {
|
||||
return $testData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForReverse
|
||||
*/
|
||||
public function testReverse($expected, $string, $encoding = null) {
|
||||
$result = S::reverse($string, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function stringsForReverse() {
|
||||
$testData = array(
|
||||
array('', ''),
|
||||
array('raboof', 'foobar'),
|
||||
array('řàb ôòf', 'fòô bàř', 'UTF-8'),
|
||||
array('∂∆ ˚åß', 'ßå˚ ∆∂', 'UTF-8')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
Reference in New Issue
Block a user