diff options
author | Florian Pritz <bluewind@xinu.at> | 2016-06-25 16:22:40 +0200 |
---|---|---|
committer | Florian Pritz <bluewind@xinu.at> | 2016-07-04 07:58:15 +0200 |
commit | 6b7fd633b9866806a37beca1e9a099a0dc24d68d (patch) | |
tree | e74ca329010f5d60fd696ce222189f93ea90b782 /application/service/files.php | |
parent | 21b263a88550d1da199a13d215ea1477d603b75a (diff) |
Add function to ellipsize text
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'application/service/files.php')
-rw-r--r-- | application/service/files.php | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/application/service/files.php b/application/service/files.php index c53448eb6..8f7595c50 100644 --- a/application/service/files.php +++ b/application/service/files.php @@ -95,6 +95,44 @@ class files { self::add_file_callback($id, $file, $filename); } + /** + * Ellipsize text to be at max $max_lines lines long. If the last line is + * not complete (strlen($text) < $filesize), drop it so that every line of + * the returned text is complete. If there is only one line, return that + * line as is and add the ellipses at the end. + * + * @param text Text to add ellipses to + * @param max_lines Number of lines the returned text should contain + * @param filesize size of the original file where the text comes from + * @return ellipsized text + */ + static public function ellipsize($text, $max_lines, $filesize) + { + $lines = explode("\n", $text); + $orig_len = strlen($text); + $orig_linecount = count($lines); + + if ($orig_linecount > 1) { + if ($orig_len < $filesize) { + // ensure we have a full line at the end + $lines = array_slice($lines, 0, -1); + } + + if (count($lines) > $max_lines) { + $lines = array_slice($lines, 0, $max_lines); + } + + if (count($lines) != $orig_linecount) { + // only add elipses when we drop at least one line + $lines[] = "..."; + } + } elseif ($orig_len < $filesize) { + $lines[count($lines) - 1] .= " ..."; + } + + return implode("\n", $lines); + } + static public function add_uploaded_file($id, $file, $filename) { self::add_file_callback($id, $file, $filename); |