Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save omurphy27/3ca51fda4bd0c1cb41f1 to your computer and use it in GitHub Desktop.
Save omurphy27/3ca51fda4bd0c1cb41f1 to your computer and use it in GitHub Desktop.
PHP truncate string - cut string after certain length - output shorter string.php
<?php
// http://stackoverflow.com/questions/3161816/php-cut-a-string-after-x-characters
function truncate_string($string,$length=70,$append="...") {
$string = trim($string);
if(strlen($string) > $length) {
$string = wordwrap($string, $length);
$string = explode("\n",$string);
$string = array_shift($string) . $append;
}
return $string;
}
?>
@collinsduzzy
Copy link

Code written 10 years ago still useful.
Thanks, Ollie.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment