diff --git a/README.md b/README.md index 359fb27..ccf3d96 100644 --- a/README.md +++ b/README.md @@ -665,6 +665,13 @@ $value = \TraderInteractive\Filter\Strings::explode('abc,def,ghi'); assert($value === ['abc', 'def', 'ghi']); ``` +#### Strings::stripControlCharacters +Aliased in the filterer as `strip-cchars`, this filter removes control characters from a given string optionally replacing them with the given replacement string +```php +$value = \TraderInteractive\Filter\Strings::stripControlCharacters("Hello\x00\tWorld\nTest\x7F", ' '); +assert($value === 'Hello World Test '); +``` + #### Strings::stripEmoji Aliased in the filterer as `strip-emoji`, this filter removes emoji characters from a given string optionally replacing them with the given replacement string ```php diff --git a/composer.json b/composer.json index 049a928..f69b4ad 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ "traderinteractive/filter-dates": "^4.0", "traderinteractive/filter-floats": "^4.0", "traderinteractive/filter-ints": "^4.0", - "traderinteractive/filter-strings": "^4.2" + "traderinteractive/filter-strings": "^4.3" }, "require-dev": { "phpunit/phpunit": "^9.0", diff --git a/src/Filterer.php b/src/Filterer.php index 229f857..9c635c4 100644 --- a/src/Filterer.php +++ b/src/Filterer.php @@ -50,6 +50,7 @@ final class Filterer implements FiltererInterface 'phone' => PhoneFilter::class . '::filter', 'redact' => '\\TraderInteractive\\Filter\\Strings::redact', 'string' => '\\TraderInteractive\\Filter\\Strings::filter', + 'strip-cchars' => Strings::class . '::stripControlCharacters', 'strip-emoji' => Strings::class . '::stripEmoji', 'strip-tags' => '\\TraderInteractive\\Filter\\Strings::stripTags', 'time-of-day' => TimeOfDayFilter::class . '::filter', diff --git a/tests/FiltererTest.php b/tests/FiltererTest.php index fa2c43a..6481218 100644 --- a/tests/FiltererTest.php +++ b/tests/FiltererTest.php @@ -649,6 +649,23 @@ function (int $input, int $fieldOneValue) : int { [], ], ], + 'strip-cchars' => [ + 'spec' => [ + 'field' => [['strip-cchars', ' ']], + ], + 'input' => [ + 'field' => "Hello\x00\tWorld\nTest\x7F", + ], + 'options' => [], + 'result' => [ + true, + [ + 'field' => 'Hello World Test ', + ], + null, + [], + ], + ], ]; }