Tejas Rana

Show limited words in PHP from string with HTML tags

Hi,
Here i have create another function called “truncate”.
With this function you can show limited words with HTML tags. I know there is a function in PHP “SUBSTR”. but the main issue i face with this is this will also remove HTML tags, which mean when any tag starts then that tags will not close. support here is my string:

<h1>HI this is a </h1> random string to show <a href="">a dummy text</a>

now when i use substr like this:

<?php
$string='<h1>HI this is a </h1> random string to show <a href="">a dummy text</a>';
echo substr($string,0,40);
?>

so in the outout it remove closing tag. this mean the rest of content become a link.
so i create this function, now this function will add the tags when any tags will not close.

<?php
function truncate($text, $length, $suffix ='', $isHTML = true) {
$i = 0;
$simpleTags=array('br'=>true,'hr'=>true,'input'=>true,'image'=>true,'link'=>true,'meta'=>true);
$tags = array();
if($isHTML){
preg_match_all('/<[^>]+>([^<]*)/', $text, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
foreach($m as $o){
if($o[0][1] - $i >= $length)
break;
$t = substr(strtok($o[0][0], " \t\n\r\0\x0B>"), 1);
// test if the tag is unpaired, then we mustn't save them
if($t[0] != '/' && (!isset($simpleTags[$t])))
$tags[] = $t;
elseif(end($tags) == substr($t, 1))
array_pop($tags);
$i += $o[1][1] - $o[0][1];
}
}
// output without closing tags
$output = substr($text, 0, $length = min(strlen($text), $length + $i));
// closing tags
$output2 = (count($tags = array_reverse($tags)) ? '</' . implode('></', $tags) . '>' : '');
// Find last space or HTML tag (solving problem with last space in HTML tag eg. <span class="new">)
$splitted = preg_split('/<.*>| /', $output, -1, PREG_SPLIT_OFFSET_CAPTURE);
$last_item = end($splitted);
$pos = end($last_item);
// Append closing tags to output
$output.=$output2;
// Get everything until last space
$one = substr($output, 0, $pos);
// Get the rest
$two = substr($output, $pos, (strlen($output) - $pos));
// Extract all tags from the last bit
preg_match_all('/<(.*?)>/s', $two, $tags);
// Add suffix if needed
if (strlen($text) > $length) { $one .= $suffix; }
// Re-attach tags
$output = $one . implode($tags[0]);
//added to remove unnecessary closure
$output = str_replace('</!-->','',$output);
return $output;
}
?>

now how to use it?

<?php
$string='<h1>HI this is a </h1> random string to show <a href="">a dummy text</a>';
echo truncate($string,40);
?>

Try it.
I hope this code will help you..

Exit mobile version