Skip to content

Instantly share code, notes, and snippets.

@michaelhoang
Created August 3, 2014 14:34
Show Gist options
  • Save michaelhoang/b610108609f5793d05de to your computer and use it in GitHub Desktop.
Save michaelhoang/b610108609f5793d05de to your computer and use it in GitHub Desktop.
Sub words with length
<?php
// Sub words with length
function truncate( $text, $length = 100, $more = null ) {
if ( null === $more )
$more = ' ...';
$text = strip_tags( $text );
$text = substr($text, 0, $length - strlen($more) + 1);
$words_array = preg_split("/[\s]+/", $text);
array_pop( $words_array );
$text = implode( ' ', $words_array );
$text .= $more;
return $text;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment