Git Product home page Git Product logo

emoji-detector-php's Introduction

Emoji Detection

This library will find all emoji in an input string and return information about each emoji character. It supports emoji with skin tone modifiers, as well as the composite emoji that are made up of multiple people.

Build Status

Installation

composer require p3k/emoji-detector

Or include src/Emoji.php in your project, and make sure the map.json and regexp.json files are available in the same folder as Emoji.php. You don't need any of the other files for use in your own projects.

Usage

Detect Emoji

$input = "Hello ๐Ÿ‘๐Ÿผ World ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ฆโ€๐Ÿ‘ฆ";
$emoji = Emoji\detect_emoji($input);

print_r($emoji);

The function returns an array with details about each emoji found in the string.

Array
(
  [0] => Array
    (
      [emoji] => ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ฆโ€๐Ÿ‘ฆ
      [short_name] => man-woman-boy-boy
      [num_points] => 7
      [points_hex] => Array
        (
          [0] => 1F468
          [1] => 200D
          [2] => 1F469
          [3] => 200D
          [4] => 1F466
          [5] => 200D
          [6] => 1F466
        )
      [hex_str] => 1F468-200D-1F469-200D-1F466-200D-1F466
      [skin_tone] =>
    )
  [1] => Array
    (
      [emoji] => ๐Ÿ‘๐Ÿผ
      [short_name] => +1
      [num_points] => 2
      [points_hex] => Array
        (
          [0] => 1F44D
          [1] => 1F3FC
        )

      [hex_str] => 1F44D-1F3FC
      [skin_tone] => skin-tone-3
    )
)
  • emoji - The emoji sequence found, as the original byte sequence. You can output this to show the original emoji.
  • short_name - The short name of the emoji, as defined by Slack's emoji data.
  • num_points - The number of unicode code points that this emoji is composed of.
  • points_hex - An array of each unicode code point that makes up this emoji. These are returned as hex strings. This will also include "invisible" characters such as the ZWJ character and skin tone modifiers.
  • hex_str - A list of all unicode code points in their hex form separated by hyphens. This string is present in the Slack emoji data array.
  • skin_tone - If a skin tone modifier was used in the emoji, this field indicates which skin tone, since the short_name will not include the skin tone.

Test if a string is a single emoji

Since simply counting the number of unicode characters in a string does not tell you how many visible emoji are in the string, determining whether a single character is an emoji is more involved. This function will return the emoji data only if the string contains a single emoji character, and false otherwise.

$emoji = Emoji\is_single_emoji('๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ฆโ€๐Ÿ‘ฆ');
print_r($emoji);
Array
(
    [emoji] => ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ฆโ€๐Ÿ‘ฆ
    [short_name] => man-woman-boy-boy
    [num_points] => 7
    [points_hex] => Array
        (
            [0] => 1F468
            [1] => 200D
            [2] => 1F469
            [3] => 200D
            [4] => 1F466
            [5] => 200D
            [6] => 1F466
        )

    [hex_str] => 1F468-200D-1F469-200D-1F466-200D-1F466
    [skin_tone] =>
)
$emoji = Emoji\is_single_emoji('๐Ÿ˜ป๐Ÿˆ');
// false

License

Copyright 2017 by Aaron Parecki.

Available under the MIT license.

Emoji data sourced from iamcal/emoji-data under the MIT license.

Emoji parsing regex sourced from EmojiOne under the MIT license.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.