my last post was a biiiiig mistake. This is the right way to display a few words of an html formatted text, which is based on the functions of the previous post, strllen_Html and strlen_noHtml, that returns the string length counting tag-characters and non-tag-characters.
This can be useful to put a preview of an article on the front page when the rest of the article is displayed on another page:
function getPreviewText($text) {
// Strip all tags
$desc = strip_tags(html_entity_decode($text), "<a><em>");
$charlen = 0; $crs = 0;
if(strlen_HTML($desc) == 0)
$preview = substr($desc, 0, 69);
else
{
$i = 0;
while($charlen < 80)
{
$crs = strpos($desc, " ", $crs)+1;
$lastopen = strrpos(substr($desc, 0, $crs), "<");
$lastclose = strrpos(substr($desc, 0, $crs), ">");
if($lastclose > $lastopen)
{
// we are not in a tag
$preview = substr($desc, 0, $crs);
$charlen = strlen_noHTML($preview);
}
$i++;
}
}
return $preview."…"
}
will display text cut as near as possible to character 80 respecting each <a> and <em> tags and ending with ...