From aced44860bc8d96d26a0e8671f7aef39709db6fd Mon Sep 17 00:00:00 2001 From: Ralf Fritzsche Date: Mon, 9 Nov 2026 15:45:41 +0100 Subject: [PATCH] feat: add StringUtil::removeZeroWidthCharacters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removes U+200B to U+200D and U+FEFF, which text pasted from mails or web pages often carries and which charsets such as Mac Roman cannot encode. 🤖 Generated with Claude Code --- src/StringUtil.php | 6 ++++++ tests/StringUtilTest.php | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/src/StringUtil.php b/src/StringUtil.php index 1f2c7e7..4dcdfe4 100644 --- a/src/StringUtil.php +++ b/src/StringUtil.php @@ -99,6 +99,12 @@ public static function normalizeLineEndings(string $input, string $to = self::WI return \Safe\preg_replace("/\r\n|\r|\n/", $to, $input); } + /** Removes U+200B to U+200D and U+FEFF, which text pasted from mails or web pages often carries. */ + public static function removeZeroWidthCharacters(string $input): string + { + return \Safe\preg_replace('/[\x{200B}-\x{200D}\x{FEFF}]/u', '', $input); + } + /** Convert string that could be in different UTF encodings (UTF-8, UTF-16BE, ...) to UTF-8. */ public static function toUTF8(string $string): string { diff --git a/tests/StringUtilTest.php b/tests/StringUtilTest.php index d856567..442c7f3 100644 --- a/tests/StringUtilTest.php +++ b/tests/StringUtilTest.php @@ -89,6 +89,14 @@ public function testNormalizeLineEndings(): void ); } + public function testRemoveZeroWidthCharacters(): void + { + self::assertSame( + 'für dringende Anliegen', + StringUtil::removeZeroWidthCharacters("für drin\u{200B}gende\u{200C} An\u{200D}liegen\u{FEFF}") + ); + } + public function testUTF8(): void { $expectedUTF8 = 'test';