A small utility to count characters and shorten posts to tweet-length.

0.2.10 2017-11-25 19:51 UTC

This package is not auto-updated.

Last update: 2021-11-27 03:28:12 UTC


Build Status

A small utility to count characters, autolink, and shorten posts to an acceptable tweet-length summary.

This is a port of the Python module of the same name. Please refer to for documentation.

Note that this module depends on the mb_ string methods to be available. I get the best results by setting


somewhere in my project.


If you're using Composer, you can simply composer require kylewm/brevity.

Otherwise, TODO



Find out how many characters a message will use on Twitter with tweetLength():

$brevity = new \Kylewm\Brevity\Brevity();
$length = $brevity->tweetLength('Published my first npm and composer packages today!');
echo $length;  // 99

This text is 119 characters but, due to wrapping, will only use 99 characters.


Convert URLs in plaintext to HTML links.

$brevity = new \Kylewm\Brevity\Brevity();
$html = $brevity->autolink("I'm a big fan of (and its creator");
echo $html;

Note that brevity handles parentheses and other punctuation as you'd expect.


The shorten($text) function takes a message of any length and shortens it to a Tweet-length of 280 characters, adding an ellipsis at the end of it is truncated. It will not truncate a word or URL in the middle. Shorten takes a few optional parameters that change the way the tweet is formed. Any of these parameters can be false.

  • $permalink - included after the ellipsis if and only if the text is shortened. Must be a URL or false.
  • $shortpermalink - included in parentheses at the end of tweets that are not shortened. Must be a URL or false.
  • $shortpermacitation - included in parentheses at the end of tweets that are not shortened. Must not be a URL, e.g. t4fT2
  • $formatAsTitle - take the text as a title of a longer article. Always formats as "Title: $permalink" or "Title… $permalink" if shortened.
$brevity = new \Kylewm\Brevity\Brevity();
$permalink = "";
$longnote = "Brevity ( shortens notes that are too long to fit in a single tweet. It can also count characters to help you make sure your note won't need to be shortened!";
$tweet = $brevity->shorten($longnote, $permalink);
echo $tweet;


  • 0.2.10 - 2017-11-25: Account for 280 character limit and multi-byte character weights. Also backports some fixes from the python library.
  • 0.2.8 - 2016-04-19: Support article+media format
  • 0.2.5 - 2016-01-29: Changed namespace from Kylewm to Kylewm\Brevity for better PSR-0 compatibility.