Truncate Text

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

First off, you don't need both:

Code: Select all

$thread_title = $out['title']; 
      if (strlen($thread_title) == 20) { 
      $thread_title .=  '...'; 
      }
and

Code: Select all

$maxlength = 20; 
      if (strlen($thread_title) > $maxlength){ 
      $truncatedtext = substr($thread_title, 0, $maxlength) . "..."; 
      } else { 
      $truncatedtext = $thread_title; 
      }

If you want you can amalgamate them into:

Code: Select all

$thread_title = $out['title'];
$maxlength = 20;
if (strlen($thread_title) == $maxlength) {
    $thread_title .= '...';
}
Then after than I would put:

Code: Select all

echo 'The truncated text is: '.$thread_title;
as a way to see what it looks like.

Mac
Post Reply