How to limit text length in PHP and provide ‘Read more’ link

If you have set your PHP site to display post excerpts on the front or home page, you will want visitors to click on the title or a link to encourage them to continue reading your post or article, right? PHP makes this technique easy, and customizable.

<?php
   $string = "Here is use big string of your paragraph or description.";
   if (strlen($string) > 150) {
       $stringCut = substr($string, 0, 15);// change 15 top what ever text length you want to show.
       $endPoint = strrpos($stringCut, ' ');
       $string = $endPoint? substr($stringCut, 0, $endPoint):substr($stringCut, 0);
       $string .= '... <a style="cursor: pointer;" href="" >Read More</a>';
   }
   echo $string;
?>

Leave a Reply

Your email address will not be published. Required fields are marked *