Skip to content

Commit 8881b3f

Browse files
authored
Merge pull request #25 from chadicus/v3.x
AGI-1181: Add Strings::stripEmoji
2 parents 15a3b0f + d558d5a commit 8881b3f

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ $value = \TraderInteractive\Filter\Strings::redact('a string with some unwanted
8989
assert($value === 'a string with some ******** *****');
9090
```
9191

92+
#### Strings::stripEmoji
93+
94+
This filter will strip emoji, pictographs, alphanumeric supplement characters and more from a given string.
95+
96+
The second, optional argument specifies a replacement string for the removed characters.
97+
98+
```php
99+
\TraderInteractive\Filter\Strings::stripTags('🙄 this is ridiculous', ' ');
100+
assert($value === ' this is ridiculous');
101+
102+
```
92103
#### Strings::stripTags
93104

94105
This filter will strip HTML, XML, and PHP tags from a string. This filter also accepts null values, which will be returned as null.

src/Filter/Strings.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,37 @@ public static function stripTags(string $value = null, string $replacement = '')
182182
return strip_tags($valueWithReplacements); // use built-in as a safeguard to ensure tags are stripped
183183
}
184184

185+
/**
186+
* Strip emoji character and various other pictograph characters
187+
*
188+
* @param string $value The input string.
189+
* @param string $replacement The string to replace the tags with. Defaults to an empty string.
190+
*
191+
* @return string;
192+
*/
193+
public static function stripEmoji(string $value, string $replacement = ''): string
194+
{
195+
$alphanumericSupplement = '/[\x{1F100}-\x{1F1FF}]/u';
196+
$pictographRegex = '/[\x{1F300}-\x{1F5FF}]/u';
197+
$emoticonRegex = '/[\x{1F600}-\x{1F64F}]/u';
198+
$transportSymbolRegex = '/[\x{1F680}-\x{1F6FF}]/u';
199+
$supplementalSymbolRegex = '/[\x{1F900}-\x{1F9FF}]/u';
200+
$miscSymbolsRegex = '/[\x{2600}-\x{26FF}]/u';
201+
$dingbatsRegex = '/[\x{2700}-\x{27BF}]/u';
202+
203+
$regexPatterns = [
204+
$alphanumericSupplement,
205+
$pictographRegex,
206+
$emoticonRegex,
207+
$transportSymbolRegex,
208+
$supplementalSymbolRegex,
209+
$miscSymbolsRegex,
210+
$dingbatsRegex,
211+
];
212+
213+
return preg_replace($regexPatterns, $replacement, $value);
214+
}
215+
185216
private static function validateMinimumLength(int $minLength)
186217
{
187218
if ($minLength < 0) {

tests/Filter/StringsTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,4 +548,48 @@ public function provideRedactFailsOnBadInput() : array
548548
],
549549
];
550550
}
551+
552+
/**
553+
* @param string $input The string value to be filtered.
554+
* @param string $expectedValue The expected filtered value.
555+
*
556+
* @test
557+
* @dataProvider provideStripEmoji
558+
*/
559+
public function stripEmoji(string $input, string $expectedValue)
560+
{
561+
$actualValue = Strings::stripEmoji($input);
562+
$this->assertSame($expectedValue, $actualValue);
563+
}
564+
565+
/**
566+
* @return array
567+
*/
568+
public static function provideStripEmoji(): array
569+
{
570+
return [
571+
'mulitple emoji' => [
572+
'input' => 'This 💩 text contains 😞 multiple emoji 🍔 characters 🍚. As well as an alphanumeric '
573+
. 'supplement 🆗 and flag 🚩',
574+
'expected' => 'This text contains multiple emoji characters . As well as an alphanumeric '
575+
. 'supplement and flag ',
576+
],
577+
'emoji' => [
578+
'input' => '🙄 this is ridiculous',
579+
'expected' => ' this is ridiculous',
580+
],
581+
'alphanumeric supplement' => [
582+
'input' => 'Contains a 🆗 character',
583+
'expected' => 'Contains a character',
584+
],
585+
'flag/transportation symbols' => [
586+
'input' => 'Contains a 🚩 character',
587+
'expected' => 'Contains a character',
588+
],
589+
'dingbat symbols' => [
590+
'input' => 'Contains a ❗ character',
591+
'expected' => 'Contains a character',
592+
],
593+
];
594+
}
551595
}

0 commit comments

Comments
 (0)