Skip to content

Commit

Permalink
Merge pull request #8 from ariaieboy/master
Browse files Browse the repository at this point in the history
fix issue #7 and #2 add feature requested in #4
  • Loading branch information
ConsoleTVs authored Sep 20, 2019
2 parents ca1d410 + 307f379 commit 8c38323
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions Classes/Blocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,18 @@ public function clean()
*/
public function badWords()
{
$words = explode(' ', $this->text);

return collect($this->dictionary)->filter(function ($value) use ($words) {
return collect($this->dictionary)->filter(function ($value) {
$matches = [];
if ($this->strict) {
return str_contains(strtolower($this->text), strtolower($value['word']));
return preg_match('/'.$value['word'].'/iu', $this->text, $matches, PREG_UNMATCHED_AS_NULL);
}
$pattern = "/\b{$value['word']}\b/iu";

return in_array(strtolower($value['word']), $words);
return preg_match($pattern, $this->text, $matches, PREG_UNMATCHED_AS_NULL);
})->map(function ($value) {
return [
'language' => $value['language'],
'word' => strtolower($value['word']),
'word' => $value['word'],
];
})->toArray();
}
Expand All @@ -149,14 +149,15 @@ public function badWords()
public function filter()
{
$bad_words = collect($this->badWords())->pluck('word')->toArray();

return collect(explode(' ', $this->text))->map(function ($value) use ($bad_words) {
$text = $this->text;
foreach ($bad_words as $word) {
if ($this->strict) {
return (str_contains(strtolower($value), $bad_words)) ? $this->blockWord($value) : $value;
$text = preg_replace('/'.$word.'/iu', $this->blockWord($word), $text);
} else {
$text = preg_replace("/\b".$word."\b/iu", $this->blockWord($word), $text);
}

return in_array(strtolower($value), $bad_words) ? $this->blockWord($value) : $value;
})->implode(' ');
}
return $text;
}

/**
Expand Down

0 comments on commit 8c38323

Please sign in to comment.