Last active
December 26, 2015 05:59
-
-
Save astockwell/7104146 to your computer and use it in GitHub Desktop.
PHP truncate text at word function
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 | |
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