From e60707f06f0d612ffb5e84004780debedcda0a4c Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Fri, 25 Jan 2013 01:46:53 +0100 Subject: [PATCH] Fixed word wrap in StringUtil::truncateHTML() Fixes #1102 --- .../files/lib/util/StringUtil.class.php | 42 +++++-------------- 1 file changed, 10 insertions(+), 32 deletions(-) diff --git a/wcfsetup/install/files/lib/util/StringUtil.class.php b/wcfsetup/install/files/lib/util/StringUtil.class.php index 4add58c760..bf1c8aa414 100644 --- a/wcfsetup/install/files/lib/util/StringUtil.class.php +++ b/wcfsetup/install/files/lib/util/StringUtil.class.php @@ -591,8 +591,17 @@ final class StringUtil { $truncatedString .= $tag[1]; // get length of the content without entities. If the content is too long, keep entities intact - $contentLength = self::length(self::decodeHTML($tag[3])); + $decodedContent = self::decodeHTML($tag[3]); + $contentLength = self::length($decodedContent); if ($contentLength + $totalLength > $length) { + if ($wordWrap) { + if (preg_match('/^(.{1,'.($length - $totalLength).'}) /s', $decodedContent, $match)) { + $truncatedString .= StringUtil::encodeHTML($match[1]); + } + + break; + } + $left = $length - $totalLength; $entitiesLength = 0; if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[3], $entities, PREG_OFFSET_CAPTURE)) { @@ -618,37 +627,6 @@ final class StringUtil { } } - // if word wrap is active search for the last word change - if ($wordWrap) { - $lastWhitespace = self::lastIndexOf($truncatedString, ' '); - // check if inside a tag - $lastOpeningTag = self::lastIndexOf($truncatedString, '<'); - if ($lastOpeningTag < $lastWhitespace) { - $lastClosingTag = self::lastIndexOf($truncatedString, '>'); - if (($lastClosingTag === false || $lastClosingTag > $lastWhitespace) && $lastClosingTag > $lastOpeningTag) { - $lastWhitespace = $lastOpeningTag; - array_shift($openTags); - if ($truncatedString[$lastWhitespace] != ' ') { - $firstPart = self::substring($truncatedString, 0, $lastWhitespace); - $secondPart = self::substring($truncatedString, $lastWhitespace + 1); - $truncatedString = $firstPart.' '.$secondPart; - } - } - } - if ($lastWhitespace !== false) { - $endString = self::substring($truncatedString, $lastWhitespace); - preg_match_all('/<\/([a-z]+)>/', $endString, $tagOverhead, PREG_SET_ORDER); - if (count($tagOverhead)) { - foreach ($tagOverhead as $tag) { - if (!in_array($tag[1], $openTags)) { - array_unshift($openTags, $tag[1]); - } - } - } - $truncatedString = self::substring($truncatedString, 0, $lastWhitespace); - } - } - // close all open tags foreach ($openTags as $tag) { $truncatedString .= ''; -- 2.20.1