Truncate a long string and add Ellipsis
Login or Register to Bookmark this snippet
This function will truncate a string to a certain length. By default it will stop on a word.
<?php
function Truncate($string, $length, $stopanywhere=false) {
//truncates a string to a certain char length, stopping on a word if not specified otherwise.
if (strlen($string) >
$length) {
//limit hit!
$string =
substr($string,
0,
($length -3));
if ($stopanywhere) {
//stop anywhere
$string .= '...';
} else{
//stop on a word.
}
}
return $string;
}
?>
Added by JC on 13th January, 2008
There are no comments about this snippet.
You must be registered and logged in to post a comment.
Login here to post a comment