Last active
February 8, 2019 17:24
-
-
Save cmkilger/4d7093d88200a34a46db89983497fa99 to your computer and use it in GitHub Desktop.
Detects if a string is made up of just emoji (and space).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function isEmojiOnly($subject) { | |
$pattern = "/^[\\s\n\r]*(?:(?:[\xc2\xa9\xc2\xae\xe2\x80\xbc\xe2\x81\x89\xe2\x84\xa2\xe2\x84\xb9\xe2\x86\x94-\xe2\x86\x99\xe2\x86\xa9-\xe2\x86\xaa\xe2\x8c\x9a-\xe2\x8c\x9b\xe2\x8c\xa8\xe2\x8f\x8f\xe2\x8f\xa9-\xe2\x8f\xb3\xe2\x8f\xb8-\xe2\x8f\xba\xe2\x93\x82\xe2\x96\xaa-\xe2\x96\xab\xe2\x96\xb6\xe2\x97\x80\xe2\x97\xbb-\xe2\x97\xbe\xe2\x98\x80-\xe2\x98\x84\xe2\x98\x8e\xe2\x98\x91\xe2\x98\x94-\xe2\x98\x95\xe2\x98\x98\xe2\x98\x9d\xe2\x98\xa0\xe2\x98\xa2-\xe2\x98\xa3\xe2\x98\xa6\xe2\x98\xaa\xe2\x98\xae-\xe2\x98\xaf\xe2\x98\xb8-\xe2\x98\xba\xe2\x99\x88-\xe2\x99\x93\xe2\x99\xa0\xe2\x99\xa3\xe2\x99\xa5-\xe2\x99\xa6\xe2\x99\xa8\xe2\x99\xbb\xe2\x99\xbf\xe2\x9a\x92-\xe2\x9a\x94\xe2\x9a\x96-\xe2\x9a\x97\xe2\x9a\x99\xe2\x9a\x9b-\xe2\x9a\x9c\xe2\x9a\xa0-\xe2\x9a\xa1\xe2\x9a\xaa-\xe2\x9a\xab\xe2\x9a\xb0-\xe2\x9a\xb1\xe2\x9a\xbd-\xe2\x9a\xbe\xe2\x9b\x84-\xe2\x9b\x85\xe2\x9b\x88\xe2\x9b\x8e-\xe2\x9b\x8f\xe2\x9b\x91\xe2\x9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-\xf0\x9f\x87\xbf]{2}|.[\xe2\x83\xa0\xe2\x83\xa3\xef\xb8\x8f]+)+[\\s\n\r]*)+$/u"; | |
return (preg_match($pattern, $subject) === 1); | |
} | |
function test($string, $isEmoji) { | |
if (isEmojiOnly($string) !== $isEmoji) { | |
echo $string." should".($isEmoji ? "" : " not")." be emoji only\n"; | |
} | |
} | |
test("Hello ๐ฒ", false); | |
test("-", false); | |
test("+", false); | |
test("$", false); | |
test(" ๐จโ๐จโ๐งโ๐ง ", true); | |
test(" 0๏ธโฃ โค๏ธ", true); | |
test("๐จโ๐จโ๐งโ๐ง โค๏ธ", true); | |
test(" 0๏ธโฃ โค๏ธ๐จโ๐จโ๐งโ๐ง ", true); | |
test("0๏ธโฃ\nโค๏ธ", true); | |
test("ยฉยฎ๐๐๐๐ฑโโโยฎยฉโข๐๐ด๐ตโซ๏ธโช๏ธ๐น๐๐ธโช๏ธโซ๏ธโป๏ธโพ๏ธ๐๐โฆ๏ธโฅ๏ธโฃ๏ธโ ๏ธ๐๏ธ๐ด๐โ๐จ๐ญ๐ฏ๐๐๐๐๐๐ฅ๐ฌโชโช๐โ๏ธ#๏ธโฃ#๏ธโฃโคต๏ธ*๏ธโฃโน๏ธโฉ๏ธโช๏ธโ๏ธโ๏ธโก๏ธ๐โ๏ธ๐ผโธโฏ4๏ธโฃ๐๐๐ถ๐๐๐ง๐๐ โ ใฝ๏ธ๐ฏโ๏ธโ๐ณ๐ฏ๐ท๐ซโโญ๏ธ๐ขโ๏ธ๐ ฑใ๏ธโด๏ธ๐๐๐๐๐๐๐๐ฉ๐๐โฑ๐๐ฟ๐๐ท๐ฌ๐กโ๐ซ๐ ๐ ๐๐ด๐ฏ๐กโฒ๐ป๐๐ธ๐พ๐ฑ๐ฒ๐ฏ๐๐๐๐๐ข๐ฅ๐ฆ๐บ๐ค๐๐ ๐๐๐๐๐๐ท๐บ๐ฑ๐๐๐ถ๐ต๐๐ก๐๐ ๐๐โ๏ธโ๐จโ๏ธโโญ๏ธ๐๐๐๐๐๐๐๐บ๐๐ธ๐๐ด๐๐๐๐๐ฆ๐๐ฝ๐ญ๐๐ถ๐๐ ๐๐จโ๐ฉโ๐งโ๐ฆ๐ฉโโค๏ธโ๐ฉ๐ฅ๐ฟ๐ฒ๐ช๐๐๐๐๐๐ด๐๐ฒ๐", true); | |
// Requires Unicode 8.0 support for skin tones and additional emoji | |
test("๐ด๐ป", true); | |
test("๐", true); | |
test("๐", true); | |
test("๐บ", true); | |
test("๐ด๐พ", true); | |
test("๐ฟ", true); | |
test("๐ญ", true); | |
test("๐๐ผ", true); | |
test("๐๐ฝ", true); | |
test("๐๐ป", true); | |
test("๐๐ป", true); | |
test("๐ค", true); | |
test("๐", true); | |
test("ใ", false); | |
test("ใท", false); | |
test("ไท", false); | |
test("ไทซ", false); | |
test("ไทฝ", false); | |
test("๊", false); |
Support gender.
Added support for flag tags.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
\p{So}\p{Sk}\p{M}
was matching a lot of things which would not be considered emoji. Now using specific code points and ranges.