Skip to content

Instantly share code, notes, and snippets.

@GarySwift
Created March 21, 2018 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GarySwift/5ce28c864a462f51482804e6644e51e5 to your computer and use it in GitHub Desktop.
Save GarySwift/5ce28c864a462f51482804e6644e51e5 to your computer and use it in GitHub Desktop.
PHP function to truncate string
<?php
# Truncate string
function truncate($string,$length=170,$append="&hellip;") {
$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