813 visitas
Truncate string to certain length without cutting the final word in half
This function takes a long string and shortens it to a defined length and adds appends an ellipsis (or custom string) to the end. Instead of chopping a word in half (if the limit finished within it), it moves the pointer up to the previous space.
PHP
- /**
- * Truncates a string to a certain length
- * @param string $text
- * @param int $limit
- * @param string $ending
- * @return string
- */
- function truncate($text, $limit = 25, $ending = '...') {
- $text = $text . $ending;
- }
- return $text;
- }
Enviado por miguelSantirso hace over 2 years — modificado por última vez hace less than a minute