steppinghat/emoji-detector

Detect and validate emoji in an input string

1.7.0 2024-12-15 07:39 UTC

README

Latest Stable Version Total Downloads License Build Status Unicode Version

Have an input string full of emoji's and you want to know detailed information about each emoji? Want to build an easy way to validate emoji's that come in as input data?

This 👏 is 👏 the 👏 library 👏 you 👏 want!

What does this thing do?

This library simply parses input strings and returns a list of relevant information about emoji's that are present in the string. It currently supports version 15.1 of the emoji standard.

Installation

Install this library using composer

$ composer require steppinghat/emoji-detector

ℹī¸ We recommend using the Caret Version Range (^) (see below for why)

Updates

Updates to the Unicode version will always be released as new minor versions. We recommend using the (^) caret version range so that new Emoji's are automatically supported in your project when running composer update.

Any breaking changes will be released as new major versions, and bugfixes will be released as new patch versions as usual.

Usage

The Model

For most outputs, the library will provide an EmojiInfo object that contains all of the relevant information about an emoji. The object will contain the following information:

All of these properties are protected, but can be accessed by their appropriate getters and setters. E.g. getCategory() or getSkinTone().

Emoji detection

<?php

require_once('vendor/autoload.php');

use SteppingHat\EmojiDetector\EmojiDetector;

$input = "Hello 👋 world!";
$detector = new EmojiDetector();
$emojis = $detector->detect($input);

print_r($emojis);

This example is the most common usage of the detector, returning an array of objects that provide detailed information about each emoji found in the input string.

Array
(
    [0] => SteppingHat\EmojiDetector\Model\EmojiInfo Object
        (
            [emoji:protected] => 👋
            [name:protected] => waving hand
            [shortName:protected] => hand-fingers-open
            [category:protected] => People & Body
            [skinTone:protected] =>
            [hexCodes:protected] => Array
                (
                    [0] => 1F44B
                )

            [offset:protected] => 6
            [mbOffset:protected] => 6
        )

)

The library has full support for complex emoji's that make use of the ZWJ (Zero Width Joiner) character.

<?php

require_once('vendor/autoload.php');

use SteppingHat\EmojiDetector\EmojiDetector;

$input = "I ❤ī¸ to 👨‍đŸ’ģ";
$detector = new EmojiDetector();
$emojis = $detector->detect($input);

print_r($emojis);

The above will produce the following output:

Array
(
    [0] => SteppingHat\EmojiDetector\Model\EmojiInfo Object
        (
            [emoji:protected] => ❤ī¸
            [name:protected] => red heart
            [shortName:protected] => emotion
            [category:protected] => Smileys & Emotion
            [skinTone:protected] => 
            [hexCodes:protected] => Array
                (
                    [0] => 2764
                    [1] => FE0F
                )

            [offset:protected] => 2
            [mbOffset:protected] => 2
        )

    [1] => SteppingHat\EmojiDetector\Model\EmojiInfo Object
        (
            [emoji:protected] => 👨‍đŸ’ģ
            [name:protected] => man technologist
            [shortName:protected] => person-role
            [category:protected] => People & Body
            [skinTone:protected] => 
            [hexCodes:protected] => Array
                (
                    [0] => 1F468
                    [1] => 200D
                    [2] => 1F4BB
                )

            [offset:protected] => 13
            [mbOffset:protected] => 9
        )

)

Detect distinct emojis

If you only want to detect distinct emojis in a string, you can use the detectDistinct method. This will return an array of distinct emojis found in the input string, and the position of the first distinct occurence of that emoji.

<?php

require_once('vendor/autoload.php');

use SteppingHat\EmojiDetector\EmojiDetector;

$detector = new EmojiDetector();

$emojis = $detector->detectDistinct("WHAT IS A KILOMETER đŸ—ŖđŸ—ŖđŸ—ŖđŸĻ…đŸĻ…đŸĻ…")

print_r($emojis);

The above will produce the following output:

(
    [0] => SteppingHat\EmojiDetector\Model\EmojiInfo Object
        (
            [emoji:protected] => đŸ—Ŗ
            [name:protected] => speaking head
            [shortName:protected] => person-symbol
            [category:protected] => People & Body
            [skinTone:protected] => 
            [hexCodes:protected] => Array
                (
                    [0] => 1F5E3
                )

            [offset:protected] => 20
            [mbOffset:protected] => 20
        )

    [1] => SteppingHat\EmojiDetector\Model\EmojiInfo Object
        (
            [emoji:protected] => đŸĻ…
            [name:protected] => eagle
            [shortName:protected] => animal-bird
            [category:protected] => Animals & Nature
            [skinTone:protected] => 
            [hexCodes:protected] => Array
                (
                    [0] => 1F985
                )

            [offset:protected] => 32
            [mbOffset:protected] => 23
        )

)

Testing for single emojis

Sometimes it is handy to test if an input string is a single emoji on it's own.

<?php

require_once('vendor/autoload.php');

use SteppingHat\EmojiDetector\EmojiDetector;

$detector = new EmojiDetector();

$detector->isSingleEmoji("💩"); // Returns TRUE
$detector->isSingleEmoji("Time to dance 🌚"); // Returns FALSE
$detector->isSingleEmoji("🍆🍒"); // Returns FALSE

Testing if a string is completely emojis

Similar to calling, isSingleEmoji, calling isEmojiString will check if a string only contains emojis.

<?php

require_once('vendor/autoload.php')

use SteppingHat\EmojiDetector\EmojiDetector;

$detector = new EmojiDetector();

$detector->isEmojiString("😂😂😂"); // Returns TRUE
$detector->isEmojiString("🏎ī¸đŸ’¨"); // Returns TRUE
$detector->isEmojiString("Deez nuts đŸĨœ"); // Returns FALSE

Tests

Included for library development purposes is a small set of test cases to assure that basic library functions work as expected. These tests can be launched by running the following:

$ composer test

To run tests against multiple PHP versions, either push to GitHub and open a PR, or preferrably use act locally:

act

License

Made with ❤ī¸ by Javan Eskander

Available for use under the MIT license.

Emoji data sourced from amio/emoji.json