Skip to content

Commit

Permalink
Update Pattern For Filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
YasserElgammal committed Nov 14, 2024
1 parent 3f0a1f3 commit 22b70d8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "yasser-elgammal/pure-text",
"description": "Filter Bad Words",
"version": "1.0.1",
"version": "1.0.2",
"license": "MIT",
"autoload": {
"psr-4": {
Expand Down
12 changes: 11 additions & 1 deletion src/Services/PureTextFilterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ public function filter($text)
$badWords = Config::get('badwords.words', []);
$replacement = Config::get('badwords.replacement', '***');

return str_ireplace($badWords, $replacement, $text);
if (empty($badWords)) {
return $text; // Return text as is if no bad words are configured
}

$pattern = '/' . implode('|', array_map(function ($word) {
return '\b' . implode('[\W_]*', array_map(function ($char) {
return $char . '{1,}';
}, preg_split('//u', preg_quote($word), -1, PREG_SPLIT_NO_EMPTY))) . '\b';
}, $badWords)) . '/iu';

return preg_replace($pattern, $replacement, $text);
}
}

0 comments on commit 22b70d8

Please sign in to comment.