Vaia Vaia - 1 month ago 8
Javascript Question

Count character length of emoji?

I want to validate the name when a new user signs up at my page. One of those checks is if the character limit isn't above 100.

But since one single emoji like

Answer

If you need to get the length in grapheme units (and NOT characters, like mb_strlen would do), you can use grapheme_strlen.

$emoji = "\u{1F469}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F48B}\u{200D}\u{1F469}";
echo $emoji , " : " , strlen($emoji) , "\n"; // 27, count byte
echo $emoji , " : " , mb_strlen($emoji) , "\n"; // 8, count characters
echo $emoji , " : " , grapheme_strlen($emoji) , "\n"; // 1, count grapheme units

https://3v4l.org/inNP9