diff --git a/src/Stringy.php b/src/Stringy.php index 7acfc00..bc3eb15 100644 --- a/src/Stringy.php +++ b/src/Stringy.php @@ -1249,6 +1249,18 @@ class Stringy implements Countable, IteratorAggregate, ArrayAccess return $array; } + /** + * Strip all whitespace characters. This includes tabs and newline + * characters, as well as multibyte whitespace such as the thin space + * and ideographic space. + * + * @return Stringy Object with whitespace stripped + */ + public function stripWhitespace() + { + return $this->regexReplace('[[:space:]]+', ''); + } + /** * Returns the substring beginning at $start with the specified $length. * It differs from the mb_substr() function in that providing a $length of diff --git a/tests/StringyTest.php b/tests/StringyTest.php index d538770..4900b81 100644 --- a/tests/StringyTest.php +++ b/tests/StringyTest.php @@ -1727,6 +1727,36 @@ class StringyTestCase extends PHPUnit_Framework_TestCase ); } + /** + * @dataProvider stripWhitespaceProvider() + */ + public function testStripWhitespace($expected, $str, $encoding = null) + { + $stringy = S::create($str, $encoding); + $result = $stringy->stripWhitespace(); + $this->assertStringy($result); + $this->assertEquals($expected, $result); + $this->assertEquals($str, $stringy); + } + + public function stripWhitespaceProvider() + { + return array( + array('foobar', ' foo bar '), + array('teststring', 'test string'), + array('Οσυγγραφέας', ' Ο συγγραφέας '), + array('123', ' 123 '), + array('', ' ', 'UTF-8'), // no-break space (U+00A0) + array('', '           ', 'UTF-8'), // spaces U+2000 to U+200A + array('', ' ', 'UTF-8'), // narrow no-break space (U+202F) + array('', ' ', 'UTF-8'), // medium mathematical space (U+205F) + array('', ' ', 'UTF-8'), // ideographic space (U+3000) + array('123', '  1  2  3  ', 'UTF-8'), + array('', ' '), + array('', ''), + ); + } + /** * @dataProvider substrProvider() */