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

Add stripWhitespace

This commit is contained in:
Viktor Persson
2016-12-20 19:40:02 +01:00
parent 0607751e17
commit d80347e72a
2 changed files with 42 additions and 0 deletions

View File

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

View File

@@ -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', '  123  ', 'UTF-8'),
array('', ' '),
array('', ''),
);
}
/**
* @dataProvider substrProvider()
*/