Skip to content

Instantly share code, notes, and snippets.

View websmith's full-sized avatar

Nate Cornelius websmith

View GitHub Profile
@ashrewdmint
ashrewdmint / gist:277528
Created January 14, 2010 21:33
Smart character limit functions for PHP
// Intelligently truncate a string to a certain number of characters.
// Each uppercase or wider-than-normal character reduces the final length.
function truncate($string, $length, $ellipsis = true) {
// Count all the uppercase and other wider-than-normal characters
$wide = strlen(preg_replace('/[^A-Z0-9_@#%$&]/', '', $string));
// Reduce the length accordingly
$length = round($length - $wide * 0.2);