Thomas Lomas

Software Engineer (Charlotte, NC, USA / Remote)

Truncate String (Character Limiter)

When displaying long pieces of user-definable text, it is sometimes useful to truncate it down into a smaller chunk for displaying in a fixed sized area.

This can be achieved with the following function:

function truncate($text, $length) {
    if(strlen($text) <= $length) return $text;
    return substr($text, 0, $length) . '...';

Example Usage


echo truncate('This is a very long piece of text', 9);


This is a…