Created
March 21, 2018 13:31
-
-
Save GarySwift/5ce28c864a462f51482804e6644e51e5 to your computer and use it in GitHub Desktop.
PHP function to truncate string
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 | |
# Truncate string | |
function truncate($string,$length=170,$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