Limit string length and display '...' : String Length « String « PHP






Limit string length and display '...'

<?php
   // Limit $summary to how many characters?
   $limit = 100;
$summary = <<< summary
Text Text Text Text Text Text Text Text Text Text Text Text 
Text Text Text Text Text Text Text Text Text 
Text Text Text Text Text Text Text Text Text Text 
Text Text Text Text Text Text Text Text Text 
Text Text Text Text Text Text Text Text Text Text 
Text Text Text Text Text Text Text Text Text Text 
summary;

   if (strlen($summary) > $limit)
      $summary = substr($summary, 0, strrpos(substr($summary, 0, $limit), ' ')) . '...';
      echo $summary;
?>


           
       








Related examples in the same category

1.Finding the Length of a String with strlen()