Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use the PHP function above. The HTML tags will be considered.</p> <pre><code>function truncate($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) { if ($considerHtml) { if (strlen(preg_replace('/&lt;.*?&gt;/', '', $text)) &lt;= $length) { return $text; } preg_match_all('/(&lt;.+?&gt;)?([^&lt;&gt;]*)/s', $text, $lines, PREG_SET_ORDER); $total_length = strlen($ending); $open_tags = array(); $truncate = ''; foreach ($lines as $line_matchings) { if (!empty($line_matchings[1])) { if (preg_match('/^&lt;(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)&gt;$/is', $line_matchings[1])) { } else if (preg_match('/^&lt;\s*\/([^\s]+?)\s*&gt;$/s', $line_matchings[1], $tag_matchings)) { $pos = array_search($tag_matchings[1], $open_tags); if ($pos !== false) { unset($open_tags[$pos]); } } else if (preg_match('/^&lt;\s*([^\s&gt;!]+).*?&gt;$/s', $line_matchings[1], $tag_matchings)) { array_unshift($open_tags, strtolower($tag_matchings[1])); } $truncate .= $line_matchings[1]; } $content_length = strlen(preg_replace('/&amp;[0-9a-z]{2,8};|&amp;#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2])); if ($total_length+$content_length &gt; $length) { $left = $length - $total_length; $entities_length = 0; if (preg_match_all('/&amp;[0-9a-z]{2,8};|&amp;#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) { foreach ($entities[0] as $entity) { if ($entity[1]+1-$entities_length &lt;= $left) { $left--; $entities_length += strlen($entity[0]); } else { break; } } } $truncate .= substr($line_matchings[2], 0, $left+$entities_length); break; } else { $truncate .= $line_matchings[2]; $total_length += $content_length; } if($total_length &gt;= $length) { break; } } } else { if (strlen($text) &lt;= $length) { return $text; } else { $truncate = substr($text, 0, $length - strlen($ending)); } } if (!$exact) { $spacepos = strrpos($truncate, ' '); if (isset($spacepos)) { $truncate = substr($truncate, 0, $spacepos); } } $truncate .= $ending; if($considerHtml) { foreach ($open_tags as $tag) { $truncate .= '&lt;/' . $tag . '&gt;'; } } return $truncate; } $str = '&lt;table&gt;&lt;tr&gt;&lt;td&gt;...blah blah text entered....&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;'; print truncate($str, 10, '', true, true); // will output: &lt;table&gt;&lt;tr&gt;&lt;td&gt;...blah bl&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload