Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

73 lines (43 sloc) 2.897 kB


~ THE TRUNCENATOR ~ is the ultimate clean syndication function (and supporting functions)

If you’re implementing POSSE to syndicate your content out to services like Twitter or Facebook, odds are you want to pre-process anything you send out to deal with problems like:

  • Length limits
  • Lack of HTML display

~ THE TRUNCENATOR ~ is a highly–configurable solution to these problems. Give it a string and some parameters and it’ll spit out something perfect for syndication.


  • Truncate text to the nearest word boundary
  • Append a URL
  • If truncation took place, append an ellipsis (customisable, defaults to …)
  • If truncation didn’t take place, surround the URL with parens


Typical usage: Syndicating a note out to Twitter.


$raw_string = 'If you’re using Chome and you quit accidentally, losing important text forever, don’t panic! It can be found in /Application Support/Google/Chrome/Default/Last Session (on a Mac) with a bit of digging around.';
$uri = ''; // Real example

$truncated = text\truncate($raw_string, $length=140, $uri=$uri, $urilen=20);

echo $truncated;
// EOF 

Results in:

If you’re using Chome and you quit accidentally, losing important text forever, don’t panic! It can be…

See the docblock on the function for more details.

Supporting Functions

If you want to expand <img> elements into just their @href instead of stripping them out completely, the expand_img() function handles that:


$raw_string = 'Hah! IndieWebCamp group animated gif: <img src="" alt="animated gif">';
$uri = ''; // Another real example

$truncated = text\truncate($raw_string, $length=140, $uri=$uri, $urilen=20);

echo $truncated;
// EOF 

Results in:

Hah! IndieWebCamp group animated gif: (


I have written a load of phpunit tests and will commit them to the github project soon.


What’s here is an initial, scruffy implementation to cater for my immediate needs. I plan on heavily refactoring bits of the code out into multiple separate functions as this is a surprisingly tricky problem to deal with.

If you have any ideas for other things ~ THE TRUNCENATOR ~ should do/how it should work, let me know or submit a pull request :)


  • Write more+better tests
  • Improve config options (some of them aren’t actually being used)
  • Refactor different stages into multiple functions
  • Add ability to preserve hashtags at the expense of text
Jump to Line
Something went wrong with that request. Please try again.