From 4bb791e31880d2f1e074ea7dc95970d88d2131ce Mon Sep 17 00:00:00 2001 From: "Daniel St. Jules" Date: Tue, 23 Jul 2013 01:11:13 -0400 Subject: [PATCH] Add reverse() --- README.md | 19 ++++++++++++------- src/Stringy/Stringy.php | 21 +++++++++++++++++++++ tests/Stringy/StringyTest.php | 19 +++++++++++++++++++ 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 93d2946..3c9e74c 100644 --- a/README.md +++ b/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 diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index ef732b2..7025154 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -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; + } + } ?> diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index 64efa79..d909b3c 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -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; + } + } ?>