Skip to content

Instantly share code, notes, and snippets.

@timnovinger
Created November 10, 2011 06:32
Show Gist options
  • Save timnovinger/1354269 to your computer and use it in GitHub Desktop.
Save timnovinger/1354269 to your computer and use it in GitHub Desktop.
ColorJar: PHP neat_trim()
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