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
// 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); | |