Skip to content

Commit e4f05eb

Browse files
authored
Replace raw invisible characters in regex expressions with counterpart Unicode regex notations (#45680)
1 parent 7906436 commit e4f05eb

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

src/Illuminate/Foundation/Http/Middleware/TrimStrings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected function transform($key, $value)
5353
return $value;
5454
}
5555

56-
return preg_replace('~^[\s​]+|[\s​]+$~u', '', $value) ?? trim($value);
56+
return preg_replace('~^[\s\x{FEFF}\x{200B}]+|[\s\x{FEFF}\x{200B}]+$~u', '', $value) ?? trim($value);
5757
}
5858

5959
/**

src/Illuminate/Support/Str.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ public static function snake($value, $delimiter = '_')
10731073
*/
10741074
public static function squish($value)
10751075
{
1076-
return preg_replace('~(\s|\x{3164})+~u', ' ', preg_replace('~^[\s]+|[\s]+$~u', '', $value));
1076+
return preg_replace('~(\s|\x{3164})+~u', ' ', preg_replace('~^[\s\x{FEFF}]+|[\s\x{FEFF}]+$~u', '', $value));
10771077
}
10781078

10791079
/**

tests/Http/Middleware/TrimStringsTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public function test_no_zero_width_space_character_returns_the_same_string()
1616
$request = new Request;
1717

1818
$request->merge([
19-
'title' => 'This title does not contains any zero-width space',
19+
'title' => 'This title does not contain any zero-width space',
2020
]);
2121

2222
$middleware = new TrimStrings;
2323

2424
$middleware->handle($request, function ($req) {
25-
$this->assertEquals('This title does not contains any zero-width space', $req->title);
25+
$this->assertEquals('This title does not contain any zero-width space', $req->title);
2626
});
2727
}
2828

@@ -34,13 +34,13 @@ public function test_leading_zero_width_space_character_is_trimmed()
3434
$request = new Request;
3535

3636
$request->merge([
37-
'title' => '​This title contains a zero-width space at the begining',
37+
'title' => '​This title contains a zero-width space at the beginning',
3838
]);
3939

4040
$middleware = new TrimStrings;
4141

4242
$middleware->handle($request, function ($req) {
43-
$this->assertEquals('This title contains a zero-width space at the begining', $req->title);
43+
$this->assertEquals('This title contains a zero-width space at the beginning', $req->title);
4444
});
4545
}
4646

@@ -70,13 +70,13 @@ public function test_leading_zero_width_non_breakable_space_character_is_trimmed
7070
$request = new Request;
7171

7272
$request->merge([
73-
'title' => 'This title contains a zero-width non-breakable space at the begining',
73+
'title' => 'This title contains a zero-width non-breakable space at the beginning',
7474
]);
7575

7676
$middleware = new TrimStrings;
7777

7878
$middleware->handle($request, function ($req) {
79-
$this->assertEquals('This title contains a zero-width non-breakable space at the begining', $req->title);
79+
$this->assertEquals('This title contains a zero-width non-breakable space at the beginning', $req->title);
8080
});
8181
}
8282

@@ -88,13 +88,13 @@ public function test_leading_multiple_zero_width_non_breakable_space_characters_
8888
$request = new Request;
8989

9090
$request->merge([
91-
'title' => 'This title contains a zero-width non-breakable space at the begining',
91+
'title' => 'This title contains a zero-width non-breakable space at the beginning',
9292
]);
9393

9494
$middleware = new TrimStrings;
9595

9696
$middleware->handle($request, function ($req) {
97-
$this->assertEquals('This title contains a zero-width non-breakable space at the begining', $req->title);
97+
$this->assertEquals('This title contains a zero-width non-breakable space at the beginning', $req->title);
9898
});
9999
}
100100

@@ -106,13 +106,13 @@ public function test_combination_of_leading_and_trailing_zero_width_non_breakabl
106106
$request = new Request;
107107

108108
$request->merge([
109-
'title' => '​This title contains a combination of zero-width non-breakable space and zero-width spaces characters at the begining and the end​',
109+
'title' => '​This title contains a combination of zero-width non-breakable space and zero-width spaces characters at the beginning and the end​',
110110
]);
111111

112112
$middleware = new TrimStrings;
113113

114114
$middleware->handle($request, function ($req) {
115-
$this->assertEquals('This title contains a combination of zero-width non-breakable space and zero-width spaces characters at the begining and the end', $req->title);
115+
$this->assertEquals('This title contains a combination of zero-width non-breakable space and zero-width spaces characters at the beginning and the end', $req->title);
116116
});
117117
}
118118
}

0 commit comments

Comments
 (0)