How can PHP be used to truncate text from a database if it exceeds a certain character limit?

When retrieving text from a database in PHP, you can use the `substr` function to truncate the text if it exceeds a certain character limit. By specifying the starting position and the length of the substring, you can easily limit the text to a specific number of characters.

// Retrieve text from the database
$text = $row['text'];

// Check if text exceeds character limit
$char_limit = 100;
if (strlen($text) > $char_limit) {
    // Truncate text if it exceeds character limit
    $truncated_text = substr($text, 0, $char_limit) . '...';
} else {
    $truncated_text = $text;
}

// Output truncated text
echo $truncated_text;