Skip to content

Commit 6df4c12

Browse files
committed
Optimize blocklist filtering by using Regex
1 parent 9390a85 commit 6df4c12

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

src/Sqids.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -623,16 +623,12 @@ public function __construct(
623623
);
624624
}
625625

626+
// Filter out blocklist words that are shorter than 3 characters or contain non-alphabet characters
626627
$filteredBlocklist = [];
627-
$alphabetChars = str_split(strtolower($alphabet));
628-
foreach ((array) $blocklist as $word) {
629-
if (strlen((string) $word) >= 3) {
630-
$wordLowercased = strtolower($word);
631-
$wordChars = str_split((string) $wordLowercased);
632-
$intersection = array_filter($wordChars, fn($c) => in_array($c, $alphabetChars));
633-
if (count($intersection) == count($wordChars)) {
634-
$filteredBlocklist[] = strtolower((string) $wordLowercased);
635-
}
628+
$alphabetPattern = '/^[' . preg_quote($alphabet, '/') . ']+$/i';
629+
foreach ($blocklist as $word) {
630+
if (strlen((string) $word) >= 3 && preg_match($alphabetPattern, $word)) {
631+
$filteredBlocklist[] = strtolower((string) $word);
636632
}
637633
}
638634

0 commit comments

Comments
 (0)