diff --git a/README.md b/README.md index b0cc51d..386ec52 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,17 @@ $value = \TraderInteractive\Filter\Strings::redact('a string with some unwanted assert($value === 'a string with some ******** *****'); ``` +#### Strings::stripEmoji + +This filter will strip emoji, pictographs, alphanumeric supplement characters and more from a given string. + +The second, optional argument specifies a replacement string for the removed characters. + +```php +\TraderInteractive\Filter\Strings::stripTags('🙄 this is ridiculous', ' '); +assert($value === ' this is ridiculous'); + +``` #### Strings::stripTags This filter will strip HTML, XML, and PHP tags from a string. This filter also accepts null values, which will be returned as null. diff --git a/src/Filter/Strings.php b/src/Filter/Strings.php index b0abd3e..3f3c345 100644 --- a/src/Filter/Strings.php +++ b/src/Filter/Strings.php @@ -182,6 +182,37 @@ public static function stripTags(string $value = null, string $replacement = '') return strip_tags($valueWithReplacements); // use built-in as a safeguard to ensure tags are stripped } + /** + * Strip emoji character and various other pictograph characters + * + * @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 stripEmoji(string $value, string $replacement = ''): string + { + $alphanumericSupplement = '/[\x{1F100}-\x{1F1FF}]/u'; + $pictographRegex = '/[\x{1F300}-\x{1F5FF}]/u'; + $emoticonRegex = '/[\x{1F600}-\x{1F64F}]/u'; + $transportSymbolRegex = '/[\x{1F680}-\x{1F6FF}]/u'; + $supplementalSymbolRegex = '/[\x{1F900}-\x{1F9FF}]/u'; + $miscSymbolsRegex = '/[\x{2600}-\x{26FF}]/u'; + $dingbatsRegex = '/[\x{2700}-\x{27BF}]/u'; + + $regexPatterns = [ + $alphanumericSupplement, + $pictographRegex, + $emoticonRegex, + $transportSymbolRegex, + $supplementalSymbolRegex, + $miscSymbolsRegex, + $dingbatsRegex, + ]; + + return preg_replace($regexPatterns, $replacement, $value); + } + private static function validateMinimumLength(int $minLength) { if ($minLength < 0) { diff --git a/tests/Filter/StringsTest.php b/tests/Filter/StringsTest.php index d20d085..871298c 100644 --- a/tests/Filter/StringsTest.php +++ b/tests/Filter/StringsTest.php @@ -547,4 +547,48 @@ public function provideRedactFailsOnBadInput() : array ], ]; } + + /** + * @param string $input The string value to be filtered. + * @param string $expectedValue The expected filtered value. + * + * @test + * @dataProvider provideStripEmoji + */ + public function stripEmoji(string $input, string $expectedValue): void + { + $actualValue = Strings::stripEmoji($input); + $this->assertSame($expectedValue, $actualValue); + } + + /** + * @return array + */ + public static function provideStripEmoji(): array + { + return [ + 'mulitple emoji' => [ + 'input' => 'This 💩 text contains 😞 multiple emoji 🍔 characters 🍚. As well as an alphanumeric ' + . 'supplement 🆗 and flag 🚩', + 'expected' => 'This text contains multiple emoji characters . As well as an alphanumeric ' + . 'supplement and flag ', + ], + 'emoji' => [ + 'input' => '🙄 this is ridiculous', + 'expected' => ' this is ridiculous', + ], + 'alphanumeric supplement' => [ + 'input' => 'Contains a 🆗 character', + 'expected' => 'Contains a character', + ], + 'flag/transportation symbols' => [ + 'input' => 'Contains a 🚩 character', + 'expected' => 'Contains a character', + ], + 'dingbat symbols' => [ + 'input' => 'Contains a ❗ character', + 'expected' => 'Contains a character', + ], + ]; + } }