SteppingHat / php-emoji-detector

Detect and validate emoji in an input string

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fails to detect repeated emoji in a string

aaronpk opened this issue Β· comments

If the input string contains the same emoji more than once, only one instance is returned in the array. Here is a failing test case:

	public function testDetectRepeatedEmoji() {
		$string = 'πŸ’©πŸ’©';
		$emojis = (new EmojiDetector())->detect($string);
		$this->assertCount(2, $emojis);
	}

Hey there!

Thanks a bunch for spotting this one and for putting together a clear test case! You're totally right; that's a bug we definitely need to squash.

I'll work on a fix and cut a release soon. Keep your eyes peeled for any updates. πŸ˜„

After looking into it a bit, this is actually intended functionality.

There is a second (albeit undocumented) parameter that solves this issue called unique, which is set to true by default.

public function testDetectRepeatedEmoji() {
    $string = 'πŸ’©πŸ’©';
    $emojis = (new EmojiDetector())->detect($string, false);
    $this->assertCount(2, $emojis);
}

When setting this second parameter to false, the test now passes.


Initially this parameter did not exist, and was added in 2022 when the isSingleEmoji() method was introduced.
Prior to that, the default behaviour was the same as now, to only show a unique list of emojis that were detected in a string.

I personally think the behaviour should flip, but this means a breaking change for anyone using the library. Curious to hear your opinion too.

If you're in a pickle, you can use that second parameter for now, and I'll update the documentation to describe it.

Oh that works then, that's fine, just adding docs for it would be helpful. I actually only found this library because I was working on updates to my own emoji library and wanted to look for an example of finding emoji that didn't use a regex.