the version of my predecessor will add $rep even if the string is shorter than max. fixed version:
<?php
function truncate($string, $max = 20, $rep = '')
{
if (strlen($string) <= ($max + strlen($rep)))
{
return $string;
}
$leave = $max - strlen ($rep);
return substr_replace($string, $rep, $leave);
}
?>
To preserve the filename extension you can call it like this:
truncate([filename], 30, '...' . end(explode('.', [filename])))