Trim extra characters from string (truncate string)

/ Published in: PHP
Save to your folder(s)

If a string exceeds x characters (defaults to 100), trims the string to the first x characters and appends a tail to the end of the truncated string (defaults to an ellipsis).

Copy this code and paste it in your HTML
  1. function trimExtraChars($string, $max = 100, $tail = '...') {
  2. if (strlen($string) > $max) {
  3. $string = substr($string, 0, $max) . $tail;
  4. }
  6. return $string;
  7. }


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.