Jump to content


Photo

If more than 10 char replace with ....?


  • Please log in to reply
2 replies to this topic

#1 Mr.x

Mr.x
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 08 July 2006 - 06:46 AM

Hello Everyone,

Just wondering if anyone knows a way so that say I have a script that prints out a result from a database and its result is more then 10 char, to stop on char 10 and add ... ie.

applecherrybanana <- Normal
applecherry... <- After

Thanks alot,

Sean

#2 Crimpage

Crimpage
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts
  • LocationBrisbane, Australia

Posted 08 July 2006 - 06:50 AM

function nicetrim ($s, $l) {
// limit the length of the given string to $MAX_LENGTH char
// If it is more, it keeps the first $MAX_LENGTH-3 characters 
// and adds "..."
// It counts HTML char such as &aacute; as 1 char.
//

  $MAX_LENGTH = $l;
  $str_to_count = html_entity_decode($s);
  if (strlen($str_to_count) <= $MAX_LENGTH) {
   return $s;
  }

  $s2 = substr($str_to_count, 0, $MAX_LENGTH - 3);
  $s2 .= "...";
  return htmlentities($s2);
}


#3 Mr.x

Mr.x
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 08 July 2006 - 06:55 AM

Thank you very much :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users