Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/StringUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
8 changes: 8 additions & 0 deletions tests/StringUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Loading