Created
November 10, 2011 06:32
-
-
Save timnovinger/1354269 to your computer and use it in GitHub Desktop.
ColorJar: PHP neat_trim()
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
function neat_trim($str, $n, $delim='...') { | |
$len = strlen($str); | |
if ($len > $n) { | |
preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches); | |
return rtrim($matches[1]) . $delim; | |
} | |
else { | |
return $str; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment