diff --git a/core/util.inc.php b/core/util.inc.php index 552266be..18b245f2 100644 --- a/core/util.inc.php +++ b/core/util.inc.php @@ -83,6 +83,22 @@ function no_escape($input) { return $input; } +// Original PHP code by Chirp Internet: www.chirp.com.au +// Please acknowledge use of this code by including this header. +function truncate($string, $limit, $break=" ", $pad="...") { + // return with no change if string is shorter than $limit + if(strlen($string) <= $limit) return $string; + + // is $break present between $limit and the end of the string? + if(false !== ($breakpoint = strpos($string, $break, $limit))) { + if($breakpoint < strlen($string) - 1) { + $string = substr($string, 0, $breakpoint) . $pad; + } + } + + return $string; +} + /** * Turn a human readable filesize into an integer, eg 1KB -> 1024 * diff --git a/ext/comment/theme.php b/ext/comment/theme.php index 8023b9d0..281bf17c 100644 --- a/ext/comment/theme.php +++ b/ext/comment/theme.php @@ -161,7 +161,7 @@ class CommentListTheme extends Themelet { $h_name = html_escape($comment->owner_name); $h_poster_ip = html_escape($comment->poster_ip); $h_timestamp = autodate($comment->posted); - $h_comment = ($trim ? substr($tfe->stripped, 0, 50) . (strlen($tfe->stripped) > 50 ? "..." : "") : $tfe->formatted); + $h_comment = ($trim ? truncate($tfe->stripped, 50) : $tfe->formatted); $i_comment_id = int_escape($comment->comment_id); $i_image_id = int_escape($comment->image_id);