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

Add reverse()

This commit is contained in:
Daniel St. Jules
2013-07-23 01:11:13 -04:00
parent 1e2f49128b
commit 4bb791e318
3 changed files with 52 additions and 7 deletions

View File

@@ -29,6 +29,7 @@ A PHP library with a variety of string manipulation functions with multibyte sup
* [surround](#surround) * [surround](#surround)
* [insert](#insert) * [insert](#insert)
* [truncate](#truncate) * [truncate](#truncate)
* [reverse](#reverse)
* [Tests](#tests) * [Tests](#tests)
* [License](#license) * [License](#license)
@@ -340,7 +341,7 @@ S::insert('fòô bà', 'ř', 6, 'UTF-8'); // 'fòô bàř'
##### truncate ##### 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 Truncates the string to a given length, while ensuring that it does not
chop words. If $substring is provided, and truncating occurs, the string 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...' 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 ## TODO
**wordWrap** => wordwrap
**reverse** => strrev
**shuffle** => str_shuffle **shuffle** => str_shuffle
**explode** => explode **explode** => explode
@@ -369,8 +376,6 @@ S::truncate('What are your plans today?', 22, '...'); // 'What are your plans...
**wordCount** => str_word_count **wordCount** => str_word_count
**isJson**
**isMultibyte** **isMultibyte**
## Tests ## Tests

View File

@@ -550,6 +550,27 @@ class Stringy {
return $truncated . $substring; 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;
}
} }
?> ?>

View File

@@ -612,6 +612,25 @@ class StringyTestCase extends PHPUnit_Framework_TestCase {
return $testData; 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;
}
} }
?> ?>