Are there any best practices for truncating and displaying text in PHP?

When truncating and displaying text in PHP, it's important to consider the length of the text being displayed and how it will be truncated. One common approach is to limit the text to a certain number of characters and add an ellipsis (...) at the end to indicate that the text has been truncated. This helps to maintain the readability of the text while still providing a summary of the content.

function truncateText($text, $limit) {
    if (strlen($text) > $limit) {
        $text = substr($text, 0, $limit);
        $text = substr($text, 0, strrpos($text, ' ')) . '...';
    }
    return $text;
}

$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
$truncatedText = truncateText($text, 20);
echo $truncatedText;