Skip to content

Commit

Permalink
Ajustado para tratar espaços dos termos Ref.: #1
Browse files Browse the repository at this point in the history
  • Loading branch information
israelmelo committed Sep 3, 2024
1 parent cae48d0 commit c54bfeb
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ public function formatText($text)
$text = trim($text);
$text = strip_tags($text);
$text = mb_strtolower($text);
$text = preg_replace("/[^a-z0-9 \s]/", "", $text);

return $text;
}
Expand Down Expand Up @@ -295,24 +294,22 @@ public function getSpamTerms($entity, $terms): array {
$fields = $this->config['fields'];
$spam_detector = [];
$found_terms = [];
$special_chars = ['@', '#', '$', '%', '^', '·', '&', '*', '(', ')', '-', '_', '=', '+', '{', '}', '[', ']', '|', ':', ';', '"', '\'', '<', '>', ',', '.', '?', '/', ' ', ''];
$special_chars = ['@', '#', '$', '%', '^', '·', '&', '*', '(', ')', '-', '_', '=', '+', '{', '}', '[', ']', '|', ':', ';', '"', '\'', '<', '>', ',', '.', '?', '/', ' '];
$special_chars = array_map(fn($char) => preg_quote($char, '/'), $special_chars);
$special_chars = '[' . implode('', $special_chars) . ']*';

foreach ($fields as $field) {
if ($value = $entity->$field) {
$lowercase_value = $this->formatText($value);

foreach ($terms as $term) {
$lowercase_term = $this->formatText($term);

foreach($special_chars as $special_char) {
$_term = implode($special_char, mb_str_split($lowercase_term));
$_term = implode("{$special_chars}", mb_str_split($lowercase_term));

$pattern = '/([^\w]|[_0-9]|^)' . preg_quote($_term, '/') . '([^\w]|[_0-9]|$)/';

if (preg_match($pattern, $lowercase_value) && !in_array($term, $found_terms)) {

$found_terms[$field][] = $term;
}
$pattern = '/([^\w]|[_0-9]|^)' . $_term . '([^\w]|[_0-9]|$)/';

if (preg_match($pattern, $lowercase_value) && !in_array($term, $found_terms)) {
$found_terms[$field][] = $term;
}
}
}
Expand Down

0 comments on commit c54bfeb

Please sign in to comment.