diff --git a/src/StringUtil.php b/src/StringUtil.php index 1f2c7e70..4dcdfe4e 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 d856567d..442c7f38 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';