Skip to content

Commit cc92dad

Browse files
deemonicclaude
andcommitted
fix: add UTF-8 guard to PatternDriver and use original text in PhoneticDriver matches
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 412806d commit cc92dad

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

src/Drivers/PatternDriver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public function detect(string $text, Dictionary $dictionary, MaskStrategyInterfa
1818
return new Result($text ?? '', $text ?? '', [], 0);
1919
}
2020

21+
if (!mb_check_encoding($text, 'UTF-8')) {
22+
$text = mb_convert_encoding($text, 'UTF-8', 'UTF-8');
23+
}
24+
2125
$matchedWords = [];
2226
$lowerText = mb_strtolower($text, 'UTF-8');
2327
$profanities = $dictionary->getProfanities();

src/Drivers/PhoneticDriver.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ public function detect(string $text, Dictionary $dictionary, MaskStrategyInterfa
9090
continue;
9191
}
9292

93+
$originalWord = mb_substr($text, $start, $length);
94+
9395
$matchedWords[] = new MatchedWord(
94-
text: $word,
96+
text: $originalWord,
9597
base: $baseWord,
9698
severity: $dictionary->getSeverity($baseWord),
9799
position: $start,

0 commit comments

Comments
 (0)