
<?php 
function truncate($text, $length)
    {
        if ($length >= \strlen($text)) {
        return $text;
        }

    return preg_replace(
            "/^(.{1,$length})(\s.*|$)/s",
            '\\1...',
            $text
        );
    }

?>