Skip to content

Instantly share code, notes, and snippets.

@joshhartman
Created February 20, 2011 22:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshhartman/836382 to your computer and use it in GitHub Desktop.
Save joshhartman/836382 to your computer and use it in GitHub Desktop.
Shorten Text Without Breaking Mid-Word
<?php
function shorten($n, $l=75, $e=' ...') {
if ($l >= strlen($n)) { return $n; }
$n = explode(' ', substr($n, 0, $l));
if (count($n)>1) { unset($n[count($n)-1]); }
return implode(' ', $n).$e;
}
echo shorten('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras gravida purus gravida enim interdum bibendum non vel enim. In et massa eu nibh pellentesque porta.');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment