Skip to content

Commit 6fdf0c9

Browse files
bug symfony#43031 [Form] Do not trim unassigned unicode characters (simonberger)
This PR was merged into the 4.4 branch. Discussion ---------- [Form] Do not trim unassigned unicode characters | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT I faced a problem with StringUtil::trim in PHP <7.3 and added a test only (for now). I probably should have created an issue instead but I thought it is better to look at the result of a test and make sure it actually is a problem. It is caused by `\pC` from the preg_replace. I am no expert for UTF-8 whitespace and cannot suggest a good bugfix. Are you interested to work around the buggy behavior of the older PHP versions? Otherwise I'll close this. Commits ------- 8e6763a [Form] Don't trim unassigned unicode characters anymore
2 parents c7ce5ca + 8e6763a commit 6fdf0c9

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/Symfony/Component/Form/Tests/Util/StringUtilTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,20 @@
1616

1717
class StringUtilTest extends TestCase
1818
{
19-
public function testTrim()
19+
public function trimProvider()
2020
{
21-
$data = ' Foo! ';
21+
return [
22+
[' Foo! ', 'Foo!'],
23+
["\u{1F92E}", "\u{1F92E}"], // unassigned character in PCRE versions of <PHP 7.3
24+
];
25+
}
2226

23-
$this->assertEquals('Foo!', StringUtil::trim($data));
27+
/**
28+
* @dataProvider trimProvider
29+
*/
30+
public function testTrim($data, $expectedData)
31+
{
32+
$this->assertSame($expectedData, StringUtil::trim($data));
2433
}
2534

2635
/**

src/Symfony/Component/Form/Util/StringUtil.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private function __construct()
3333
*/
3434
public static function trim($string)
3535
{
36-
if (null !== $result = @preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $string)) {
36+
if (null !== $result = @preg_replace('/^[\pZ\p{Cc}\p{Cf}]+|[\pZ\p{Cc}\p{Cf}]+$/u', '', $string)) {
3737
return $result;
3838
}
3939

0 commit comments

Comments
 (0)