Created
August 3, 2014 14:34
-
-
Save michaelhoang/b610108609f5793d05de to your computer and use it in GitHub Desktop.
Sub words with length
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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