Help with counting message length!


#1

Hi all!

I have 2 questions:

  1. I’m trying to post tweets from my website. When I count message length with Javascript (length property of textarea) and with PHP (mb_strlen) I get 140. But when I post this message via Twitter API (codebird) I get an error code 186 - “Status is over 140 characters”.
    I tried to normalize message with php normalizer but this doesn’t help.
    So, how do I count message length so it would be definitely the same length as twitter sees it?

  2. Also, there is another problem: If I post just message without media (picture) - I dont get any errors. But if I post the same message with a photo I get error code 186 again. So, the second question is:
    How does picture affects the resulting message length?

Thank you very much for any response!


#2

When you post a tweet with a media id you have to shorten it by the length of characters_reserved_per_media from help/configuration


#3

Is there a reliable way to count the number of characters so as it Twitter does?
For example ":family_man_woman_girl_boy:"
Javascript: “:family_man_woman_girl_boy:”.length is 11
Twitter: the length of “:family_man_woman_girl_boy:” is 7
Is there a library of javascript functions for developers that can be used on a web-page to coordinate such calculations?
Another example is URL. What is in Twitter considered as an URL?
I am interested not in pages of instructions and advices, but a specific algorithm the same as uses Twitter.


#4

Check the twitter-text library.


#5

The length of "\uD83D\uDC68\u200D\uD83D\uDC69\u200D\uD83D\uDC66\u200D\uD83D\uDC66"
differs when you put :family_man_woman_boy_boy:
in Twitter and when you send from https://twitter.com/intent/tweet?text=👨‍👩‍👦‍👦
Something wrong with the calculations.

Where is the “twitter-text library”? And where to find the right algorithm?


#6

Here’s the twitter-text library: https://github.com/twitter/twitter-text


#7

Thanks.
Tweet length is txt.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/mg, ' ').length
I have not been experimenting with API but I can say that length of the unicode-string from my example is different in Twitter.


#8

There’s more on how Twitter counts characters in the developer docs here.


#9

not enough

It seems necessary to report a bug


#10

Feel free to raise an issue on the twitter-text GitHub project with specific details and code samples.