Skip to content

Instantly share code, notes, and snippets.

@astockwell
Last active December 26, 2015 05:59
Show Gist options
  • Save astockwell/7104146 to your computer and use it in GitHub Desktop.
Save astockwell/7104146 to your computer and use it in GitHub Desktop.
PHP truncate text at word function
<?php
function truncate($string, $length, $tailing_char = '...') {
if (strlen($string) > $length):
$string = preg_replace('/\s+?(\S+)?$/', '', substr($string . ' ', 0, $length)) . $tailing_char;
endif;
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment