Hello PHP Gurus
I found this function online. It truncates the html without breaking tags like
The problem I’m having with it, however, is that it truncates the string without respect the last word in the string.
For example, if the word is occupied, it will make it occup.
I tried adding:
[php]$html = preg_replace(’/\W\w+\s*(\W*)$/’, ‘$1’, $html);[/php]
By the end of it, and while it works 80% of the time, if the last “word” is an html tag, i.e. [tt][/tt], it will just break the tag.
How can I alter this function so that it removes the final word, without breaking the html.
[php]function zcTruncateText($html, $maxLength, $padding_element = ‘[ … ]’, $padding_anchor_href = ‘’, $padding_anchor_title = ‘’)
{
mb_internal_encoding(“UTF-8”);
$printedLength = 0;
$position = 0;
$tags = array();
ob_start();
while ($printedLength < $maxLength && preg_match('{</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;}', $html, $match, PREG_OFFSET_CAPTURE, $position)){
list($tag, $tagPosition) = $match[0];
// Print text leading up to the tag.
$str = mb_strcut($html, $position, $tagPosition - $position);
if ($printedLength + mb_strlen($str) > $maxLength){
print(mb_strcut($str, 0, $maxLength - $printedLength));
$printedLength = $maxLength;
break;
}
print($str);
$printedLength += mb_strlen($str);
if ($tag[0] == '&'){
// Handle the entity.
print($tag);
$printedLength++;
}
else{
// Handle the tag.
$tagName = $match[1][0];
if ($tag[1] == '/'){
// This is a closing tag.
$openingTag = array_pop($tags);
assert($openingTag == $tagName); // check that tags are properly nested.
print($tag);
}
else if ($tag[mb_strlen($tag) - 2] == '/'){
// Self-closing tag.
print($tag);
}
else{
// Opening tag.
print($tag);
$tags[] = $tagName;
}
}
// Continue after the tag.
$position = $tagPosition + mb_strlen($tag);
}
// Print any remaining text.
if ($printedLength < $maxLength && $position < mb_strlen($html))
print(mb_strcut($html, $position, $maxLength - $printedLength));
// Close any open tags.
while (!empty($tags))
printf('</%s>', array_pop($tags));
$bufferOuput = ob_get_contents();
ob_end_clean();
$html = $bufferOuput;
//$html = preg_replace('/\W\w+\s*(\W*)$/', '$1', $html);
// Add "read more" link if truncation occurs...
if (strlen($html) >= $maxLength)
{
if(!empty($padding_element))
{
// will this padding_element be an anchor?
if (!empty($padding_anchor_href))
$html = $html . ' <span class="logreg"><a href="'. $padding_anchor_href .'"'. (!empty($padding_anchor_title) ? ' title="'. $padding_anchor_title .'"' : '') .' style="white-space:nowrap; font-weight: bold;" rel="nofollow">'. $padding_element .'</a></span>';
else
$html .= ' ' . $padding_element;
}
}
return $html;
}[/php]