/ Published in: PHP

This function will truncate strings only at word breaks which can be used to show a teaser for complete article without breaking words.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// Original PHP code by Chirp Internet: www.chirp.com.au // Please acknowledge use of this code by including this header. function myTruncate($string, $limit, $break=".", $pad="...") { // return with no change if string is shorter than $limit return $string; // is $break present between $limit and the end of the string? } } return $string; } /***** Example ****/ $short_string=myTruncate($long_string, 100, ' ');
Comments
