Friday, August 24, 2012

Twitter Lifts 140 Character Limit !

http://blog.pay4tweet.com/2012/04/27/twitter-lifts-140-character-limit/-206


There used to be a time on Twitter when tweets were limited to 140 characters. Not any more. Most of the character counters you see when posting tweets are no longer valid. Why? In a word, T.CO.


Because Twitter now applies its t.co url shortener to EVERY string of text that looks like a domain, every domain is counted as 20 characters. 19 for the t.co shortened URL, and 1 for the space. So, if you shorten your URL to 12 characters using bit.ly, for instance, AND leave out the “http://”, your URL is still counted as twenty characters.


For example the following tweet (minus the quotes) is exactly 140 characters:

“I am a tweet. I have exactly 140 characters. If you want to make sure that I only have 140 characters you can check for yourself. 0123456789″

However, if you replace the last 20 characters with a really long URL as in the following example, and submit it at Twitter.com, the resulting 178 character string will be accepted.

“I am a tweet. I have exactly 140 characters. If you want to make sure that I only have 140 characters you can check for http://www.pay4tweet.com/buy.php?this_is_a_really_long_url”

In this case, Twitter internally replaces the URL with one shortened with their t.co url shortening service, but displays your URL, up to a certain point. It seems that Twitter crops long URLs after 50 or so characters, but you can still get past the once immovable 140 character limit. So, that last tweet actually has more than 140 characters, but as far as Twitter is concerned, it’s right at the limit.


Although t.co has been long discussed since its introduction, this particular impact on tweet length seems to have been only noticed by a few hardcore Twitter developers.


“Where is this documented?”, you may ask. Well, after hours of searching, I finally found an official notification explaining it, “How to Post Shortened Links (URLs)”, on the Twitter support site. I don’t recall any formal announcement about the change in how tweet length is measured. Because of the seemingly underground nature of this change, though, I imagine there are probably more than a few Twitter clients that will not post your 140+ character tweet according to the new guidelines. 


Additionally, some will not work if you submit a 140 character tweet with a URL that’s shorter than 20 characters.


NOTE: The rest of this post is heavily geared towards programming. Escape now, while you’re still awake. Or even better, go buy a tweet.


Counting to 140 is a pretty simple task as far as programming is concerned. However, checking for the myriad forms that a domain name can take and coding to account for counting each of those formats as 20 characters is just as complex as counting to 140 is easy.


Fortunately, after another half-day of searching, testing, and prodding, I found that Twitter supports an undocumented javascript library called twitter-text.js that can perform all the functions you’ll need.  You can find it referenced in the Twitter Developer article “How Twitter wraps URLs with t.co”.


Although the javascript library isn’t very well documented, it does have some useful functions. If you’re a programmer, and you don’t have a spare few days to come up with your own reliable way of finding domains in text strings that works better than this Twitter approved library, your time might be best spent figuring out how to decipher the twitter-text.js library.


In the end, what you have to do, in order to make an accurate character count, is


1.  Use the extractUrlsWithIndices() method:

twttr.txt.extractUrlsWithIndices(tweetText)

to return an array of all of the URLs in the tweet.


2.  Loop through the array of URLs returned by the method.


3. For each URL found, replace it in the tweet with a 20 character placeholder and place it in a replacement string.


The replacement string will be the actual length of the final tweet that Twitter will measure against its 140 character limit.


I built a javascript function called charactersleft() that does all of this and it seems to be working pretty reliably.

function charactersleft(tweet) { var url, i, lenUrlArr; var virtualTweet = tweet; var filler = "01234567890123456789"; var extractedUrls = twttr.txt.extractUrlsWithIndices(tweet); var remaining = 140; lenUrlArr = extractedUrls.length; if ( lenUrlArr > 0 ) { for (var i = 0; i < lenUrlArr; i++) { url = extractedUrls[i].url; virtualTweet = virtualTweet.replace(url,filler); } } remaining = remaining - virtualTweet.length; return remaining; }


Using this function on the two tweets listed earlier in this article will return “0″ for both tweets.

The first line in the function is just a declaration of variables that are used later.

var url, i, lenUrlArr, lenUrl;

The second line initializes a second variable “virtualTweet” that will be used to keep track of the version of the tweet that has the number of characters that Twitter will be looking for.

var virtualTweet = tweet;

Line three creates a variable that will be used as the 20 character placeholder that will replace any urls that are found.

var filler = "01234567890123456789";

The next line is where the magic happens. Here we use the ‘twttr’ object defined in Twitter’s twitter-text.js file to create an array of all the URLs in the submitted tweet string. We’ll use this array to find out where the placeholders go.

var extractedUrls = twttr.txt.extractUrlsWithIndices(tweet);

The “remaining” variable will help us keep track of how many characters we can still use in the version of the tweet that Twitter sees, which is different than characters remaining in the actual submitted tweet text.

var remaining = 140;

The next two lines help create the condition that tells us that there is a link in the tweet. First we count how many links there are. Then, if there are none, we do nothing. If there is one or more links, then we go to the next step.lenUrlArr = extractedUrls.length; if ( lenUrlArr > 0 ) {


The next few lines loop through the array “extractedUrls”, grab each extracted url, then replaces the link in our Twitter text emulator variable “virtualTweet”.

if ( lenUrlArr > 0 ) {         for (var i = 0; i < lenUrlArr; i++) {                 url = extractedUrls[i].url;                 virtualTweet = virtualTweet.replace(url,filler);         } }

The last two lines set the tweet length that Twitter sees inside of the “remaining” variable, and returns that value.

remaining = remaining - virtualTweet.length; return remaining;

As you can see, counting the length of a tweet, is a lot easier with Twitter’s magical twitter-text.js file.

Now that there is a way to get past the 140 character limit, it will be interesting to see how many clever uses people will find for the extra characters.

This entry was posted in Uncategorized. Bookmark the permalink. Comments are closed, but you can leave a trackback: Trackback URL.

No comments: