MaggsWeb:7

www.maggsweb.com

trimtext with display

July 23, 2007 | Comments | PHP

Text too long…? Not wrapping, or pushing other elements around…? I wrote this little function to trim text to any length, adding ‘…’ at the end if trimmed. Then, it displays the full text as you hover over it. Cooool!

<?php
function trimtext ($text,$max) {
  if (strlen($text) <= $max) {
    return $text;
  }
 
  $text2 = "<abbr title=".$text.">";
  $text2 = substr($text,0,($max - 3));
  $text2 .= "...</abbr>";
  return $text2;
 }
 
$text = '123456789';
echo trimtext($text,6);
?>

Displays: 123.. (Hover over for full text)

Share this:
  • Print
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • LinkedIn
  • Twitter

Write a Comment

Let me know what you think?