Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can it detect a string is all emoji or not? #3

Open
mana-ai opened this issue Oct 21, 2019 · 9 comments
Open

Can it detect a string is all emoji or not? #3

mana-ai opened this issue Oct 21, 2019 · 9 comments

Comments

@mana-ai
Copy link

mana-ai commented Oct 21, 2019

Hi Sir, thanks for this useful repo.

I wanna detect a string is all emoji or not, take an example:

😄😄 -> yes
😄 ff -> not

So how to achieve that using this repo? thanks in advance!

@petehouston
Copy link
Owner

You can use parse.unemojify() which is to convert the emoji string info text format, then verify the text.

parser.unemojify('I ❤️ ☕'); // returns: 'I :heart: :coffee:'

Now, if all words are emojis then each should start with a : and end with a :; otherwise, it is not an emoji.

@mana-ai
Copy link
Author

mana-ai commented Oct 27, 2019

@petehouston What about text in the middle?

such as 😄😄hahah😄

@mana-ai
Copy link
Author

mana-ai commented Oct 27, 2019

@petehouston Wanna it contains continuesly pure emoji then mark it as true, otherwise treat as normal.

@dm8tr
Copy link

dm8tr commented Sep 11, 2020

You can use parse.unemojify() which is to convert the emoji string info text format, then verify the text.

parser.unemojify('I ❤️ ☕'); // returns: 'I :heart: :coffee:'

Now, if all words are emojis then each should start with a : and end with a :; otherwise, it is not an emoji.

Maybe there is a better way to do this but following @petehouston comment I did something like this

import 'package:flutter_emoji/flutter_emoji.dart';

/*code*/

bool isAllEmoji(String text) {
  for (String s in EmojiParser().unemojify(text).split(" ")) if (!s.startsWith(":") || !s.endsWith(":")) return false;
  return true;
}

@mitinKevadiya
Copy link

You can use parse.unemojify() which is to convert the emoji string info text format, then verify the text.

parser.unemojify('I ❤️ ☕'); // returns: 'I :heart: :coffee:'

Now, if all words are emojis then each should start with a : and end with a :; otherwise, it is not an emoji.

Maybe there is a better way to do this but following @petehouston comment I did something like this

import 'package:flutter_emoji/flutter_emoji.dart';

/*code*/

bool isAllEmoji(String text) {
  for (String s in EmojiParser().unemojify(text).split(" ")) if (!s.startsWith(":") || !s.endsWith(":")) return false;
  return true;
}

I think we have check one more condition for this

import 'package:flutter_emoji/flutter_emoji.dart';

/*code*/

bool isAllEmoji(String text) {
    for (String s in EmojiParser().unemojify(text).split(" ")) {
      if (!s.startsWith(":") || !s.endsWith(":")) return false;
    }
    List<String> parseEmoji = EmojiParser().parseEmojis(text);
    if(parseEmoji.isNotEmpty) {
      String text1 = text;
      for(String s in parseEmoji) {
        text1 = text1.replaceAll(s, "");
      }
      if(text1.isNotEmpty) {
        return false;
      }
    }
    return true;
  }

@salaryazdjerdi
Copy link

This is the correct solution.

  bool hasOnlyEmojis(String input) {
    // find all emojis
    final emojis = EmojiParser().parseEmojis(input);

    // return if none found
    if (emojis.isEmpty) return false;

    // remove all emojis from the input
    for (final emoji in emojis) {
      input = input.replaceAll(emoji, "");
    }

    // remove all whitespace (optional)
    input = input.replaceAll(" ", "");

    // return true if nothing else left
    return input.isEmpty;
  }

@migalv
Copy link

migalv commented Dec 14, 2022

All of this works, unless you use the emojis that have color 👍🏻

UPDATE

This works:

final emojisRegExp =
    RegExp(r"(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])");

bool isOnlyEmojis(String text) {
    // find all emojis
    final emojis = emojisRegExp.allMatches(text);

    // return if none found
    if (emojis.isEmpty) return false;

    // remove all emojis from the this
    for (final emoji in emojis) {
      text = text.replaceAll(emoji.input.substring(emoji.start, emoji.end), "");
    }

    // remove all whitespace (optional)
    text = text.replaceAll(" ", "");

    // return true if nothing else left
    return text.isEmpty;
  }

  /// True if the string is only emojis and the number of emojis is less than [maxEmojis]
  bool isLessEmojisThan(String text, int maxEmojis) {
    final allEmojis = emojisRegExp.allMatches(text);
    final numEmojis = allEmojis.length;

    if (numEmojis < maxEmojis && isOnlyEmojis(text)) {
      return true;
    }

    return false;
  }

@yalda-student
Copy link

All of this works, unless you use the emojis that have color 👍🏻

UPDATE

This works:

final emojisRegExp =
    RegExp(r"(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])");

bool isOnlyEmojis(String text) {
    // find all emojis
    final emojis = emojisRegExp.allMatches(text);

    // return if none found
    if (emojis.isEmpty) return false;

    // remove all emojis from the this
    for (final emoji in emojis) {
      text = text.replaceAll(emoji.input.substring(emoji.start, emoji.end), "");
    }

    // remove all whitespace (optional)
    text = text.replaceAll(" ", "");

    // return true if nothing else left
    return text.isEmpty;
  }

  /// True if the string is only emojis and the number of emojis is less than [maxEmojis]
  bool isLessEmojisThan(String text, int maxEmojis) {
    final allEmojis = emojisRegExp.allMatches(text);
    final numEmojis = allEmojis.length;

    if (numEmojis < maxEmojis && isOnlyEmojis(text)) {
      return true;
    }

    return false;
  }

your code helps me. TNX

@shivanshukumar
Copy link

shivanshukumar commented Mar 8, 2024

All of this works, unless you use the emojis that have color 👍🏻

UPDATE

This works:

final emojisRegExp =
    RegExp(r"(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])");

bool isOnlyEmojis(String text) {
    // find all emojis
    final emojis = emojisRegExp.allMatches(text);

    // return if none found
    if (emojis.isEmpty) return false;

    // remove all emojis from the this
    for (final emoji in emojis) {
      text = text.replaceAll(emoji.input.substring(emoji.start, emoji.end), "");
    }

    // remove all whitespace (optional)
    text = text.replaceAll(" ", "");

    // return true if nothing else left
    return text.isEmpty;
  }

  /// True if the string is only emojis and the number of emojis is less than [maxEmojis]
  bool isLessEmojisThan(String text, int maxEmojis) {
    final allEmojis = emojisRegExp.allMatches(text);
    final numEmojis = allEmojis.length;

    if (numEmojis < maxEmojis && isOnlyEmojis(text)) {
      return true;
    }

    return false;
  }

still does not work for many emojis with skin tone set or for all emojis. Need a way to expand the list of emojis to match and support skin tone match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants