steppinghat / emoji-detector
Detect and validate emoji in an input string
Installs: 250 759
Dependents: 1
Suggesters: 0
Security: 0
Stars: 15
Watchers: 1
Forks: 2
Open Issues: 0
Requires
- php: >=7.1
- ext-json: *
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ^9.5
- symfony/var-dumper: ^5.4
This package is auto-updated.
Last update: 2024-12-15 07:48:20 UTC
README
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
- Emoji detection
- Detect distinct emojis
- Testing for single emojis
- Testing if a string is completely emojis
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