[PHP] Counting Twitter characters

Quoting their official docs

To the human eye the length is clearly four characters. Depending on how the data is represented this could be either five or six UTF-8 bytes. Twitter does not want to penalize a user for the fact we use UTF-8 or for the fact that the API client in question used the longer representation. Therefore, Twitter does count “cafĂ©” as four characters no matter which representation is sent.

The code to get a string’s length (like twitter) in php is to use the multi-byte strlen function


mb_strlen(utf8_decode(trim($tweet)), 'utf-8');

strlen would give the same result after using the decode function.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>