diff --git a/README.md b/README.md index f1bb5d7..b10c335 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,18 @@ $value = \TraderInteractive\Filter\Strings::redact('a string with some unwanted assert($value === 'a string with some ******** *****'); ``` +#### Strings::stripControlCharacters + +This filter will strip all ASCII control characters, plus the delete character, including tabs and newlines. +It will also strip unicode invisible formatting characters, private use characters, and unused code points. + +The second, optional argument specifies a replacement string for the removed characters. + +```php +$value = \TraderInteractive\Filter\Strings::stripControlCharacters("Hello\x00\tWorld\nTest\x7F"); +assert($value === 'HelloWorldTest'); +``` + #### Strings::stripEmoji This filter will strip emoji, pictographs, alphanumeric supplement characters and more from a given string. diff --git a/src/Filter/Strings.php b/src/Filter/Strings.php index 3f3c345..a6bc289 100644 --- a/src/Filter/Strings.php +++ b/src/Filter/Strings.php @@ -213,6 +213,27 @@ public static function stripEmoji(string $value, string $replacement = ''): stri return preg_replace($regexPatterns, $replacement, $value); } + /** + * Strip control characters from a given string + * + * @param string $value The input string. + * @param string $replacement The string to replace the tags with. Defaults to an empty string. + * + * @return string; + */ + public static function stripControlCharacters(string $value, string $replacement = ''): string + { + $asciiControlCharacters = '\x00-\x1F\x7F'; + $unicodeControlCharacters = '\p{C}'; + $controlCharactersPattern = "/[{$asciiControlCharacters}{$unicodeControlCharacters}]/u"; + $filtered = preg_replace($controlCharactersPattern, $replacement, $value); + if ($filtered === null) { + throw new FilterException("Unable to strip control characters from '{$value}'"); + } + + return $filtered; + } + private static function validateMinimumLength(int $minLength) { if ($minLength < 0) { diff --git a/tests/Filter/StringsTest.php b/tests/Filter/StringsTest.php index 871298c..e088f31 100644 --- a/tests/Filter/StringsTest.php +++ b/tests/Filter/StringsTest.php @@ -553,6 +553,7 @@ public function provideRedactFailsOnBadInput() : array * @param string $expectedValue The expected filtered value. * * @test + * @covers ::stripEmoji * @dataProvider provideStripEmoji */ public function stripEmoji(string $input, string $expectedValue): void @@ -591,4 +592,51 @@ public static function provideStripEmoji(): array ], ]; } + + /** + * @param string $input The string value to be filtered. + * @param string $expectedValue The expected filtered value. + * + * @test + * @covers ::stripControlCharacters + * @dataProvider provideStripControlCharacters + */ + public function stripControlCharacters(string $input, string $expectedValue): void + { + $actualValue = Strings::stripControlCharacters($input); + $this->assertSame($expectedValue, $actualValue); + } + + /** + * @return array + */ + public static function provideStripControlCharacters(): array + { + return [ + 'string contains no control characters' => [ + 'input' => 'a b c', + 'expected' => 'a b c', + ], + 'Contains a null byte, tab, newline, and delete' => [ + 'input' => "Hello\x00\tWorld\nTest\x7F", + 'expected' => 'HelloWorldTest', + ], + 'Contains a Zero-Width Space (\u{200B}) and a Soft Hyphen (\u{00AD})' => [ + 'input' => "Uni\u{200B}code\u{00AD}Text", + 'expected' => 'UnicodeText', + ], + ]; + } + + /** + * @test + * @covers ::stripControlCharacters + */ + public function stripControlCharactersNonUtf8() + { + $windows1252String = "100 \x80"; + $this->expectException(FilterException::class); + $this->expectExceptionMessage("Unable to strip control characters from '{$windows1252String}'"); + Strings::stripControlCharacters($windows1252String); + } }