Home

Awesome

brevity-php

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 https://github.com/kylewm/brevity for documentation.

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

mb_internal_encoding('UTF-8');

somewhere in my project.

Installation

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

Otherwise, TODO

Usage

tweetLength($text)

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 www.npmjs.com/package/brevity and composer packagist.org/packages/kylewm/brevity packages today!');
echo $length;  // 99

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

autolink($text)

Convert URLs in plaintext to HTML links.

$brevity = new \Kylewm\Brevity\Brevity();
$html = $brevity->autolink("I'm a big fan of https://en.wikipedia.org/wiki/Firefly_(TV_series) (and its creator https://en.wikipedia.org/wiki/Joss_Whedon)");
echo $html;

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

shorten($text)

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.

$brevity = new \Kylewm\Brevity\Brevity();
$permalink = "https://kylewm.com/2016/01/brevity-shortens-notes";
$longnote = "Brevity (github.com/kylewm/brevity-php) 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;

Changes