Skip to content

Commit 2020c2d

Browse files
authored
Merge pull request #4094 from ytetsuro/fix/NewlinesAtTheEndAreIncludInThePatternMatch
2 parents b28cd47 + 9bdba2e commit 2020c2d

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

system/Validation/FormatRules.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public function alpha_space(?string $value = null): bool
4444
return true;
4545
}
4646

47-
return (bool) preg_match('/^[A-Z ]+$/i', $value);
47+
// @see https://regex101.com/r/LhqHPO/1
48+
return (bool) preg_match('/\A[A-Z ]+\z/i', $value);
4849
}
4950

5051
/**
@@ -56,7 +57,8 @@ public function alpha_space(?string $value = null): bool
5657
*/
5758
public function alpha_dash(?string $str = null): bool
5859
{
59-
return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
60+
// @see https://regex101.com/r/XfVY3d/1
61+
return (bool) preg_match('/\A[a-z0-9_-]+\z/i', $str);
6062
}
6163

6264
/**
@@ -72,7 +74,8 @@ public function alpha_dash(?string $str = null): bool
7274
*/
7375
public function alpha_numeric_punct($str)
7476
{
75-
return (bool) preg_match('/^[A-Z0-9 ~!#$%\&\*\-_+=|:.]+$/i', $str);
77+
// @see https://regex101.com/r/6N8dDY/1
78+
return (bool) preg_match('/\A[A-Z0-9 ~!#$%\&\*\-_+=|:.]+\z/i', $str);
7679
}
7780

7881
/**
@@ -96,7 +99,8 @@ public function alpha_numeric(?string $str = null): bool
9699
*/
97100
public function alpha_numeric_space(?string $str = null): bool
98101
{
99-
return (bool) preg_match('/^[A-Z0-9 ]+$/i', $str);
102+
// @see https://regex101.com/r/0AZDME/1
103+
return (bool) preg_match('/\A[A-Z0-9 ]+\z/i', $str);
100104
}
101105

102106
/**
@@ -123,7 +127,8 @@ public function string($str = null): bool
123127
*/
124128
public function decimal(?string $str = null): bool
125129
{
126-
return (bool) preg_match('/^[-+]?[0-9]{0,}\.?[0-9]+$/', $str);
130+
// @see https://regex101.com/r/HULifl/1/
131+
return (bool) preg_match('/\A[-+]?[0-9]{0,}\.?[0-9]+\z/', $str);
127132
}
128133

129134
/**
@@ -147,7 +152,7 @@ public function hex(?string $str = null): bool
147152
*/
148153
public function integer(?string $str = null): bool
149154
{
150-
return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
155+
return (bool) preg_match('/\A[\-+]?[0-9]+\z/', $str);
151156
}
152157

153158
/**
@@ -181,7 +186,8 @@ public function is_natural_no_zero(?string $str = null): bool
181186
*/
182187
public function numeric(?string $str = null): bool
183188
{
184-
return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
189+
// @see https://regex101.com/r/bb9wtr/1
190+
return (bool) preg_match('/\A[\-+]?[0-9]*\.?[0-9]+\z/', $str);
185191
}
186192

187193
/**
@@ -253,6 +259,7 @@ public function valid_json(string $str = null): bool
253259
*/
254260
public function valid_email(string $str = null): bool
255261
{
262+
// @see https://regex101.com/r/wlJG1t/1/
256263
if (function_exists('idn_to_ascii') && defined('INTL_IDNA_VARIANT_UTS46') && preg_match('#\A([^@]+)@(.+)\z#', $str, $matches))
257264
{
258265
$str = $matches[1] . '@' . idn_to_ascii($matches[2], 0, INTL_IDNA_VARIANT_UTS46);

tests/system/Validation/FormatRulesTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,10 @@ public function alphaSpaceProvider()
424424
FormatRulesTest::ALPHABET . ' ',
425425
true,
426426
],
427+
[
428+
FormatRulesTest::ALPHABET . "\n",
429+
false,
430+
],
427431
[
428432
FormatRulesTest::ALPHABET . '1',
429433
false,
@@ -506,6 +510,10 @@ public function alphaNumericPunctProvider()
506510
FormatRulesTest::ALPHANUMERIC . '`',
507511
false,
508512
],
513+
[
514+
FormatRulesTest::ALPHANUMERIC . "\n",
515+
false,
516+
],
509517
[
510518
FormatRulesTest::ALPHANUMERIC . '@',
511519
false,
@@ -599,6 +607,10 @@ public function alphaNumericSpaceProvider()
599607
' ' . FormatRulesTest::ALPHANUMERIC . '-',
600608
false,
601609
],
610+
[
611+
' ' . FormatRulesTest::ALPHANUMERIC . "\n",
612+
false,
613+
],
602614
[
603615
null,
604616
false,
@@ -636,6 +648,10 @@ public function alphaDashProvider()
636648
FormatRulesTest::ALPHANUMERIC . '-\ ',
637649
false,
638650
],
651+
[
652+
FormatRulesTest::ALPHANUMERIC . "-\n",
653+
false,
654+
],
639655
[
640656
null,
641657
false,
@@ -722,6 +738,10 @@ public function numericProvider()
722738
'+42',
723739
true,
724740
],
741+
[
742+
"+42\n",
743+
false,
744+
],
725745
[
726746
'123a',
727747
false,
@@ -771,6 +791,10 @@ public function integerProvider()
771791
'-1',
772792
true,
773793
],
794+
[
795+
"+42\n",
796+
false,
797+
],
774798
[
775799
'123a',
776800
false,
@@ -824,6 +848,10 @@ public function decimalProvider()
824848
'0',
825849
true,
826850
],
851+
[
852+
"0\n",
853+
false,
854+
],
827855
[
828856
'1.0a',
829857
false,

0 commit comments

Comments
 (0)