PHP 8.3.22 Released!

Voting

: min(six, four)?
(Example: nine)

The Note You're Voting On

spcl dot delivery at gmail dot com
16 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