note 77256 modified in function.strlen by danbrown
| From: | danbrown@php.net | Date: | Thu, 12 Feb 2009 03:13:11 +0000 |
| Subject: | note 77256 modified in function.strlen by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-150533@lists.php.net to get a copy of this message | ||
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 ...
--was--
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 ...
http://php.net/manual/en/function.strlen.php