Last active
August 29, 2015 13:55
-
-
Save luckyshot/e46404a74c667acaade0 to your computer and use it in GitHub Desktop.
Trim Text nicely
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
/** | |
* Trimtext | |
* This function will trim text without cutting it in | |
* the middle of the word and adding … if longer | |
*/ | |
public function trimText($text, $length) { | |
$words = explode(" ", $text); | |
$text = ""; | |
foreach ($words as $word) { | |
if (strlen($text." ".$word) <= $length) { | |
$text .= " ".$word; | |
}else{ | |
$text .= "…"; | |
break; | |
} | |
} | |
return trim( $text ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment