mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-10 07:16:44 +02:00
Add stripWhitespace
This commit is contained in:
@@ -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
|
||||
|
@@ -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()
|
||||
*/
|
||||
|
Reference in New Issue
Block a user