Well here is my first so Labs post.
Often times, there are fields on forms or on pages that need to fit in a certain length. Article previews and select lists are two good examples. An easy way to ensure your text is withing the required length using PHP is to use
$string = substr($string, 0, $length);. This has a number of disadvantages. Firstly, words can be cut at the end so you could end up with mixed meaning. For example, consider if the string "I hate sextants" was trimmed at 10 characters...you could end up with angry clients.
There is no built in PHP function to intelligently trim strings, so I wrote one. This function ensures strings are no longer than a maximum length and shows ellipses if trimming has occured. The function can be set to trim the string at the end, or to trim in the middle. (note: both of these trimming options assume left-to-right text, if someone wants to adapt the function for right-to-left languages feel free) In either case, the function tries its hardest not to cut any words in half.