PHP 8.4.24 Released!

Voting

: three plus three?
(Example: nine)

The Note You're Voting On

spcl dot delivery at gmail dot com
18 years ago
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])))

<< Back to user notes page

To Top