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

Add toSpaces() and toTabs()

This commit is contained in:
Daniel St. Jules
2013-07-18 23:42:38 -04:00
parent 493a194612
commit 031e9e68b2
3 changed files with 95 additions and 4 deletions

View File

@@ -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**

View File

@@ -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);
}
}
?>

View File

@@ -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;
}
}
?>