The following PHP function will output text as a preview – the same way WordPress delivers short excerpts for every post. Although it is customary to follow each excerpt with three dots, you can customise the trailing text by altering the $etc variable. Call this function by using ttruncat($text,$numb), where $text is the text you want to truncate, and $numb is the number of characters that you want to output.
<?php
function ttruncat($text,$numb) {
if (strlen($text) > $numb) {
$text = substr($text, 0, $numb);
$text = substr($text,0,strrpos($text," "));
$etc = " ...";
$text = $text.$etc;
}
return $text;
}
?>
If you liked this article, you may also like:

