Created
May 21, 2014 17:55
-
-
Save omurphy27/3ca51fda4bd0c1cb41f1 to your computer and use it in GitHub Desktop.
PHP truncate string - cut string after certain length - output shorter string.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
<?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; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code written 10 years ago still useful.
Thanks, Ollie.