Are there any best practices or guidelines for handling text truncation in PHP to maintain readability and user experience?

When truncating text in PHP, it is important to consider the readability and user experience. One common approach is to truncate the text at a certain character limit while ensuring that the truncated text ends at a word boundary to maintain readability.

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

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