Created
October 3, 2017 06:34
-
-
Save patrick-mota/cd402e371a0f839717ad73de4cee6d43 to your computer and use it in GitHub Desktop.
truncate words php
This file contains hidden or 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
public function truncate($string, $length=100, $append="…") { | |
$string = trim($string); | |
if(strlen($string) > $length) { | |
$string = wordwrap($string, $length); | |
$string = explode("\n", $string, 2); | |
$string = $string[0] . $append; | |
} | |
return $string; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment