note 77256 deleted from function.strlen by hholzgra
| From: | hholzgra@php.net | Date: | Thu, 02 Aug 2012 12:15:17 +0000 |
| Subject: | note 77256 deleted from function.strlen by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189616@lists.php.net to get a copy of this message | ||
Note Submitter: mail4adry at inwind dot it
----
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:
<?php
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 ...