Snippets v2

Languages
My Snippets

Limit a string to a certain character length grace Login or Register to Bookmark this snippet

Description PHP

This function takes a long string of text and limits it to x many characters, stopping on a full world and appending '...' to the returned string.

The Code Download
  1. <?php
  2. function Truncate($string,$length) {
  3.     //truncates a string to a certain char length, stopping on a word.
  4.     if (strlen($string) > $length) {
  5.         //limit! but also stop on a complete word..
  6.         $string = substr($string,0,($length -3));
  7.         $string = substr($string,0,strrpos($string,' ')).'...';
  8.     }
  9.     return $string;
  10. }
  11. ?>
Credits Contact JC

Added by JC on 12th November, 2007

Comments

There are no comments about this snippet.

Post Comment HTML is allowed

You must be registered and logged in to post a comment.

Login here to post a comment