Skip to content

AGI-1181: Add Strings::stripEmoji #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 10, 2025
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
31 changes: 31 additions & 0 deletions src/Filter/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
44 changes: 44 additions & 0 deletions tests/Filter/StringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we do sanity checks if it strips multiple emojis properly in a message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i can add that

'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',
],
];
}
}