Skip to content

Commit 3619681

Browse files
authored
Merge pull request #20 from crazyfactory/prevent-emoji-validator
feat(latin validator): exclude c0 latin chars
2 parents f33acc7 + bae722c commit 3619681

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/LatinCharValidator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@
44

55
class LatinCharValidator
66
{
7+
/**
8+
* exclude C0 controls (basic latin): https://unicode-table.com/en/blocks/basic-latin/
9+
*
10+
* @param string $txt
11+
*
12+
* @return bool
13+
*/
714
public static function isValid(string $txt): bool
815
{
9-
return empty($txt) || preg_match('/^[\p{Latin}\p{Common}]+$/u', $txt) > 0;
16+
return empty($txt) || (preg_match('/^[\p{Latin}\p{Common}]+$/u', $txt) > 0
17+
&& preg_match('/[\x{0000}-\x{001F}]/u', $txt) == 0);
1018
}
1119
}

tests/unit/LatinCharValidatorTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,37 @@ public function testIsValid()
5454
$this->assertFalse(LatinCharValidator::isValid('الْأَبْجَدِيَّة الْعَرَبِيَّة‎'));
5555
// Russian chars
5656
$this->assertFalse(LatinCharValidator::isValid('Меня зовут Мандли'));
57+
// Basic Latin C0 controls
58+
$c0 = [
59+
'',
60+
'',
61+
'',
62+
'',
63+
'',
64+
'',
65+
' ',
66+
' ',
67+
'',
68+
'',
69+
'',
70+
'',
71+
'',
72+
'',
73+
'',
74+
'',
75+
'',
76+
'',
77+
'',
78+
'',
79+
'',
80+
'',
81+
'',
82+
'',
83+
'',
84+
''
85+
];
86+
foreach ($c0 as $item) {
87+
$this->assertFalse(LatinCharValidator::isValid('Hello World ' . $item));
88+
}
5789
}
5890
}

0 commit comments

Comments
 (0)