diff --git a/README.md b/README.md index 2275bdc..2e03fc4 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ A PHP library with a variety of string manipulation functions with multibyte sup * [padBoth](#padboth) * [startsWith](#startswith) * [endsWith](#endswith) + * [toSpaces](#tospaces) + * [toTabs](#totabs) * [Tests](#tests) * [License](#license) @@ -265,12 +267,31 @@ by setting $caseSensitive to false. S::endsWith('FÒÔ bàřs', 'àřs', true, 'UTF-8'); // true ``` +##### toSpaces + +S::toSpaces(string $str, [, int $tabLength = 4 ]) + +Converts each tab in a string to some number of spaces, as defined by +$tabLength. By default, each tab is converted to 4 consecutive spaces. + +```php +S:::toSpaces(' String speech = "Hi"') \\ ' String speech = "Hi"' +``` + +##### toTabs + +S::toTabs(string $str, [, int $tabLength = 4 ]) + +Converts each occurence of some consecutive number of spaces, as defined +by $tabLength, to a tab. By default, each 4 consecutive spaces are +converted to a tab. + +```php +S:::toTabs(" fòô bàř") \\ " fòô bàř" +``` + ## TODO -**toSpaces** - -**toTabs** - **toAnchor** **slugify** diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index 83a9d4f..4d063c6 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -421,6 +421,35 @@ class Stringy { return $substring === $endOfStr; } + /** + * Converts each tab in a string to some number of spaces, as defined by + * $tabLength. By default, each tab is converted to 4 consecutive spaces. + * + * @param string $str String to convert tabs to spaces + * @param int $tabLength Number of spaces to replace each tab with + * @return string String with tabs switched to spaces + */ + public static function toSpaces($str, $tabLength = 4) { + $spaces = str_repeat(' ', $tabLength); + + return str_replace("\t", $spaces, $str); + } + + /** + * Converts each occurence of some consecutive number of spaces, as defined + * by $tabLength, to a tab. By default, each 4 consecutive spaces are + * converted to a tab. + * + * @param string $str String to convert spaces to tabs + * @param int $tabLength Number of spaces to replace with a tab + * @return string String with spaces switched to tabs + */ + public static function toTabs($str, $tabLength = 4) { + $spaces = str_repeat(' ', $tabLength); + + return str_replace($spaces, "\t", $str); + } + } ?> diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index ebf2348..e8d4d47 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -444,6 +444,47 @@ class StringyTestCase extends PHPUnit_Framework_TestCase { return $testData; } + /** + * @dataProvider stringsForToSpaces + */ + public function testToSpaces($expected, $string, $tabLength = 4) { + $result = S::toSpaces($string, $tabLength); + $this->assertEquals($expected, $result); + } + + public function stringsForToSpaces() { + $testData = array( + array(' foo bar ', ' foo bar '), + array(' foo bar ', ' foo bar ', 5), + array(' foo bar ', ' foo bar ', 2), + array('foobar', ' foo bar ', 0), + array(" foo\n bar", " foo\n bar"), + array(" fòô\n bàř", " fòô\n bàř") + ); + + return $testData; + } + + /** + * @dataProvider stringsForToTabs + */ + public function testToTabs($expected, $string, $tabLength = 4) { + $result = S::toTabs($string, $tabLength); + $this->assertEquals($expected, $result); + } + + public function stringsForToTabs() { + $testData = array( + array(' foo bar ', ' foo bar '), + array(' foo bar ', ' foo bar ', 5), + array(' foo bar ', ' foo bar ', 2), + array(" foo\n bar", " foo\n bar"), + array(" fòô\n bàř", " fòô\n bàř") + ); + + return $testData; + } + } ?>