mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-12 00:04:11 +02:00
Fix for collapseWhitespace() not being able to handle multibyte space characters
This commit is contained in:
11
README.md
11
README.md
@@ -97,7 +97,7 @@ of the former is the following:
|
|||||||
|
|
||||||
```php
|
```php
|
||||||
use Stringy\Stringy as S;
|
use Stringy\Stringy as S;
|
||||||
echo S::create("Fòô Bàř", 'UTF-8')->collapseWhitespace()->swapCase(); // 'fÒÔ bÀŘ'
|
echo S::create('Fòô Bàř', 'UTF-8')->collapseWhitespace()->swapCase(); // 'fÒÔ bÀŘ'
|
||||||
```
|
```
|
||||||
|
|
||||||
`Stringy\Stringy` contains a __toString() method, which returns the current
|
`Stringy\Stringy` contains a __toString() method, which returns the current
|
||||||
@@ -108,8 +108,8 @@ Using the static wrapper, an alternative is the following:
|
|||||||
|
|
||||||
```php
|
```php
|
||||||
use Stringy\StaticStringy as S;
|
use Stringy\StaticStringy as S;
|
||||||
$string = S::collapseWhitespace("Fòô Bàř", 'UTF-8');
|
$string = S::collapseWhitespace('Fòô Bàř', 'UTF-8');
|
||||||
echo S::swapCase($string, 'UTF-8'); // 'fÒÔ bÀŘ''
|
echo S::swapCase($string, 'UTF-8'); // 'fÒÔ bÀŘ'
|
||||||
```
|
```
|
||||||
|
|
||||||
## Methods
|
## Methods
|
||||||
@@ -151,10 +151,11 @@ S::camelize('Camel-Case'); // 'camelCase'
|
|||||||
|
|
||||||
$stringy->collapseWhitespace()
|
$stringy->collapseWhitespace()
|
||||||
|
|
||||||
S::collapseWhitespace(string $str)
|
S::collapseWhitespace(string $str [, string $encoding ])
|
||||||
|
|
||||||
Trims the string and replaces consecutive whitespace characters with a
|
Trims the string and replaces consecutive whitespace characters with a
|
||||||
single space. This includes tabs and newline characters.
|
single space. This includes tabs and newline characters, as well as
|
||||||
|
multibyte whitespace such as the thin space and ideographic space.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
S::create(' Ο συγγραφέας ')->collapseWhitespace();
|
S::create(' Ο συγγραφέας ')->collapseWhitespace();
|
||||||
|
@@ -142,14 +142,15 @@ class StaticStringy
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Trims the string and replaces consecutive whitespace characters with a
|
* Trims the string and replaces consecutive whitespace characters with a
|
||||||
* single space. This includes tabs and newline characters.
|
* single space. This includes tabs and newline characters, as well as
|
||||||
|
* multibyte whitespace such as the thin space and ideographic space.
|
||||||
*
|
*
|
||||||
* @param string $str The string to cleanup whitespace
|
* @param string $str The string to cleanup whitespace
|
||||||
* @return string The trimmed string with condensed whitespace
|
* @return string The trimmed string with condensed whitespace
|
||||||
*/
|
*/
|
||||||
public static function collapseWhitespace($str)
|
public static function collapseWhitespace($str, $encoding = null)
|
||||||
{
|
{
|
||||||
return $result = Stringy::create($str)->collapseWhitespace()->str;
|
return $result = Stringy::create($str, $encoding)->collapseWhitespace()->str;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -249,16 +249,21 @@ class Stringy
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Trims the string and replaces consecutive whitespace characters with a
|
* Trims the string and replaces consecutive whitespace characters with a
|
||||||
* single space. This includes tabs and newline characters.
|
* single space. This includes tabs and newline characters, as well as
|
||||||
|
* multibyte whitespace such as the thin space and ideographic space.
|
||||||
*
|
*
|
||||||
* @return Stringy Object with a trimmed $str and condensed whitespace
|
* @return Stringy Object with a trimmed $str and condensed whitespace
|
||||||
*/
|
*/
|
||||||
public function collapseWhitespace()
|
public function collapseWhitespace()
|
||||||
{
|
{
|
||||||
$stringy = self::create($this->str, $this->encoding);
|
$regexEncoding = mb_regex_encoding();
|
||||||
$stringy->str = preg_replace('/\s+/u', ' ', $stringy->trim());
|
mb_regex_encoding($this->encoding);
|
||||||
|
|
||||||
return $stringy;
|
$stringy = self::create($this->str, $this->encoding);
|
||||||
|
$stringy->str = mb_ereg_replace('[[:space:]]+', ' ', $stringy);
|
||||||
|
mb_regex_encoding($regexEncoding);
|
||||||
|
|
||||||
|
return $stringy->trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -173,6 +173,8 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
array('test string', 'test string'),
|
array('test string', 'test string'),
|
||||||
array('Ο συγγραφέας', ' Ο συγγραφέας '),
|
array('Ο συγγραφέας', ' Ο συγγραφέας '),
|
||||||
array('123', ' 123 '),
|
array('123', ' 123 '),
|
||||||
|
array('1 2 3', ' 1 2 3 ', 'UTF-8'), // ideographic spaces
|
||||||
|
array('', ' ', 'UTF-8'), // thin space and space
|
||||||
array('', ' '),
|
array('', ' '),
|
||||||
array('', ''),
|
array('', ''),
|
||||||
);
|
);
|
||||||
|
@@ -111,9 +111,9 @@ class StaticStringyTestCase extends CommonTest
|
|||||||
/**
|
/**
|
||||||
* @dataProvider stringsForCollapseWhitespace
|
* @dataProvider stringsForCollapseWhitespace
|
||||||
*/
|
*/
|
||||||
public function testCollapseWhitespace($expected, $str)
|
public function testCollapseWhitespace($expected, $str, $encoding = null)
|
||||||
{
|
{
|
||||||
$result = S::collapseWhitespace($str);
|
$result = S::collapseWhitespace($str, $encoding);
|
||||||
$this->assertInternalType('string', $result);
|
$this->assertInternalType('string', $result);
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
}
|
}
|
||||||
|
@@ -145,9 +145,9 @@ class StringyTestCase extends CommonTest
|
|||||||
/**
|
/**
|
||||||
* @dataProvider stringsForCollapseWhitespace
|
* @dataProvider stringsForCollapseWhitespace
|
||||||
*/
|
*/
|
||||||
public function testCollapseWhitespace($expected, $str)
|
public function testCollapseWhitespace($expected, $str, $encoding = null)
|
||||||
{
|
{
|
||||||
$stringy = S::create($str);
|
$stringy = S::create($str, $encoding);
|
||||||
$result = $stringy->collapseWhitespace();
|
$result = $stringy->collapseWhitespace();
|
||||||
$this->assertInstanceOf('Stringy\Stringy', $result);
|
$this->assertInstanceOf('Stringy\Stringy', $result);
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
|
Reference in New Issue
Block a user