From 8d6ab6dbee8378d84c1ac0ee0ba1780ee04c6c24 Mon Sep 17 00:00:00 2001 From: chadicus Date: Mon, 10 Mar 2025 13:35:00 -0400 Subject: [PATCH 1/4] Add Strings::stripEmoji --- README.md | 11 +++++++++++ src/Filter/Strings.php | 31 +++++++++++++++++++++++++++++ tests/Filter/StringsTest.php | 38 ++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) 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..bce7f79 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 = '') + { + $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..f570e82 100644 --- a/tests/Filter/StringsTest.php +++ b/tests/Filter/StringsTest.php @@ -547,4 +547,42 @@ 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) + { + $actualValue = Strings::stripEmoji($input); + $this->assertSame($expectedValue, $actualValue); + } + + /** + * @return array + */ + public static function provideStripEmoji(): array + { + return [ + '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', + ], + ]; + } } From 79847970f1fd928ebbcfe888b9409b9081ed20a4 Mon Sep 17 00:00:00 2001 From: Chad Gray <1182337+chadicus@users.noreply.github.com> Date: Mon, 10 Mar 2025 14:14:40 -0400 Subject: [PATCH 2/4] Add missing return type hint Co-authored-by: philbti <141263723+philbti@users.noreply.github.com> --- src/Filter/Strings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Filter/Strings.php b/src/Filter/Strings.php index bce7f79..3f3c345 100644 --- a/src/Filter/Strings.php +++ b/src/Filter/Strings.php @@ -190,7 +190,7 @@ public static function stripTags(string $value = null, string $replacement = '') * * @return string; */ - public static function stripEmoji(string $value, string $replacement = '') + public static function stripEmoji(string $value, string $replacement = ''): string { $alphanumericSupplement = '/[\x{1F100}-\x{1F1FF}]/u'; $pictographRegex = '/[\x{1F300}-\x{1F5FF}]/u'; From 56d5360e615d8fd24ebb435ee7f44f80cc25d2e6 Mon Sep 17 00:00:00 2001 From: Chad Gray <1182337+chadicus@users.noreply.github.com> Date: Mon, 10 Mar 2025 14:25:19 -0400 Subject: [PATCH 3/4] Missing void return type hint Co-authored-by: philbti <141263723+philbti@users.noreply.github.com> --- tests/Filter/StringsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Filter/StringsTest.php b/tests/Filter/StringsTest.php index f570e82..a537641 100644 --- a/tests/Filter/StringsTest.php +++ b/tests/Filter/StringsTest.php @@ -555,7 +555,7 @@ public function provideRedactFailsOnBadInput() : array * @test * @dataProvider provideStripEmoji */ - public function stripEmoji(string $input, string $expectedValue) + public function stripEmoji(string $input, string $expectedValue): void { $actualValue = Strings::stripEmoji($input); $this->assertSame($expectedValue, $actualValue); From 90e1d7ad046722337c6e5835d6431705e4cc4512 Mon Sep 17 00:00:00 2001 From: chadicus Date: Mon, 10 Mar 2025 14:35:33 -0400 Subject: [PATCH 4/4] Add test case that covers multiple emojis --- tests/Filter/StringsTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Filter/StringsTest.php b/tests/Filter/StringsTest.php index a537641..871298c 100644 --- a/tests/Filter/StringsTest.php +++ b/tests/Filter/StringsTest.php @@ -567,6 +567,12 @@ public function stripEmoji(string $input, string $expectedValue): void 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',