How can PHP developers implement a feature to truncate text and provide a "read more" link for longer content displayed on a webpage?

To truncate text and provide a "read more" link for longer content displayed on a webpage, PHP developers can use the substr() function to limit the length of the text displayed. They can then check if the text exceeds a certain length and display a "read more" link that expands the full content when clicked.

<?php
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";

$maxLength = 100;

if(strlen($text) > $maxLength){
    $truncatedText = substr($text, 0, $maxLength);
    echo $truncatedText . "... <a href='#'>Read more</a>";
} else {
    echo $text;
}
?>