What are some best practices for handling words when limiting the display of characters in PHP?

When limiting the display of characters in PHP, it's important to handle words properly to avoid cutting them off in the middle. One common approach is to use the `substr` function to limit the number of characters displayed, while also checking if the last character is a space and adjusting the substring accordingly to avoid cutting off words.

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

// Example usage
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
$limitedText = limitDisplayText($text, 20);
echo $limitedText;