Jump to content

Archived

This topic is now archived and is closed to further replies.

Mr.x

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

Recommended Posts

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

Share this post


Link to post
Share on other sites
[code]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);
}[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.