diff options
| author | Michael <savetheinternet@tinyboard.org> | 2013-01-18 17:14:45 -0800 |
|---|---|---|
| committer | Michael <savetheinternet@tinyboard.org> | 2013-01-18 17:14:45 -0800 |
| commit | ced540a66ee223ba2e3e2b8dbaaf50e5b48a13d8 (patch) | |
| tree | 70989c2cf770cde5f1cea7ec2b642ee97a683342 /inc/display.php | |
| parent | df667896cddfedaa4bf228fbb321bb1195cd9345 (diff) | |
| parent | b63d94838da090cddb98f1b7f19268a32c6a75d8 (diff) | |
Merge pull request #91 from Macil/truncfixes
Post Truncation fixes
Diffstat (limited to 'inc/display.php')
| -rw-r--r-- | inc/display.php | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/inc/display.php b/inc/display.php index 264fde60..85c21e7c 100644 --- a/inc/display.php +++ b/inc/display.php @@ -155,6 +155,11 @@ function truncate($body, $url, $max_lines = false, $max_chars = false) { $max_lines = $config['body_truncate']; if ($max_chars === false) $max_chars = $config['body_truncate_char']; + + // We don't want to risk truncating in the middle of an HTML comment. + // It's easiest just to remove them all first. + $body = preg_replace('/<!--.*?-->/s', '', $body); + $original_body = $body; $lines = substr_count($body, '<br/>'); @@ -165,7 +170,7 @@ function truncate($body, $url, $max_lines = false, $max_chars = false) { $body = $m[0]; } - $body = substr($body, 0, $max_chars); + $body = mb_substr($body, 0, $max_chars); if ($body != $original_body) { // Remove any corrupt tags at the end @@ -190,9 +195,12 @@ function truncate($body, $url, $max_lines = false, $max_chars = false) { // remove broken HTML entity at the end (if existent) $body = preg_replace('/&[^;]+$/', '', $body); + $tags_no_close_needed = array("colgroup", "dd", "dt", "li", "optgroup", "option", "p", "tbody", "td", "tfoot", "th", "thead", "tr", "br", "img"); + // Close any open tags foreach ($tags as &$tag) { - $body .= "</{$tag}>"; + if (!in_array($tag, $tags_no_close_needed)) + $body .= "</{$tag}>"; } } else { // remove broken HTML entity at the end (if existent) |
