Skip to content

Commit

Permalink
Merge pull request #8297 from Sesquipedalian/censor_case
Browse files Browse the repository at this point in the history
Improves handling of letter case in word censor
  • Loading branch information
Sesquipedalian authored Jul 20, 2024
2 parents 8b02c9c + 50d25bd commit 3eb33e0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
30 changes: 29 additions & 1 deletion Sources/Lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,35 @@ public static function censorText(string &$text, bool $force = false): string
}

// Censoring isn't so very complicated :P.
$text = preg_replace($censor_vulgar, $censor_proper, $text);
foreach ($censor_vulgar as $i => $pattern) {
$text = preg_replace_callback(
$pattern,
function ($matches) use ($censor_proper, $i) {
// Special case to return the original word unchanged.
if ($censor_proper[$i] === '$0') {
return $matches[0];
}

// If the replacement contains any letters, try to match case.
if (preg_match('/\p{L}/u', $censor_proper[$i])) {
// If original was all uppercase, return uppercase.
if (Utils::strtoupper($matches[0]) === $matches[0]) {
return Utils::strtoupper($censor_proper[$i]);
}

// If original started with a capital letter, return with a capital letter.
$first_vulgar_char = Utils::entitySubstr($matches[0], 0, 1);

if (Utils::ucfirst($first_vulgar_char) === $first_vulgar_char) {
return Utils::ucfirst($censor_proper[$i]);
}
}

return $censor_proper[$i];
},
$text,
);
}

return $text;
}
Expand Down
8 changes: 8 additions & 0 deletions Sources/Unicode/SpoofDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ public static function enhanceWordCensor(string $text): void
$vulgar = explode("\n", Config::$modSettings['censor_vulgar']);
$proper = explode("\n", Config::$modSettings['censor_proper']);

if (!empty(Config::$modSettings['censorIgnoreCase'])) {
$text = Utils::convertCase($text, 'fold');

foreach ($vulgar as $i => $v) {
$vulgar[$i] = Utils::convertCase($v, 'fold');
}
}

$text_chars = preg_split('/(.)/su', $text, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

foreach ($text_chars as $text_char) {
Expand Down

0 comments on commit 3eb33e0

Please sign in to comment.