Created
February 6, 2012 11:52
-
-
Save melat0nin/1751730 to your computer and use it in GitHub Desktop.
PHP: Truncate text to x chars
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
function truncate($string,$words) { | |
$truncated = preg_replace('/((\w+\W*){' . $words . '}(\w+))(.*)/', '${1}', $string); | |
if ( strlen($string) > strlen($truncated) ) { | |
$truncated .= '...'; | |
} | |
return $truncated; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment