Truncate text without trimming HTML

This line contains 78 characters with HTML and 39 characters without HTML:

<p>I really like the <a href="http://google.com">Google</a> search engine.</p> 

I want to trim this line based on the number of characters other than HTML, so for example, if I wanted to truncate the line above to 24 characters, the output would be as follows:

 I really like the <a href="http://google.com">Google</a> 

Truncation did not consider html when determining the number of disabled characters; it only took into account the shared amount. However, he did not leave open HTML tags.

+4
source share
1 answer

Ok, here is what I put together and seems to work:

 function truncate_html($string, $length, $postfix = '&hellip;', $isHtml = true) { $string = trim($string); $postfix = (strlen(strip_tags($string)) > $length) ? $postfix : ''; $i = 0; $tags = []; // change to array() if php version < 5.4 if($isHtml) { preg_match_all('/<[^>]+>([^<]*)/', $string, $tagMatches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); foreach($tagMatches as $tagMatch) { if ($tagMatch[0][1] - $i >= $length) { break; } $tag = substr(strtok($tagMatch[0][0], " \t\n\r\0\x0B>"), 1); if ($tag[0] != '/') { $tags[] = $tag; } elseif (end($tags) == substr($tag, 1)) { array_pop($tags); } $i += $tagMatch[1][1] - $tagMatch[0][1]; } } return substr($string, 0, $length = min(strlen($string), $length + $i)) . (count($tags = array_reverse($tags)) ? '</' . implode('></', $tags) . '>' : '') . $postfix; } 

Using:

 truncate_html('<p>I really like the <a href="http://google.com">Google</a> search engine.</p>', 24); 

The function was grabbed (made a small modification):

http://www.dzone.com/snippets/truncate-text-preserving-html

+8
source

Source: https://habr.com/ru/post/1432332/


All Articles