note 77256 added to function.strlen
| From: | mail4adry at inwind dot it | Date: | Tue, 21 Aug 2007 20:53:42 +0000 |
| Subject: | note 77256 added to function.strlen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-129680@lists.php.net to get a copy of this message | ||
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 ...
----
Server IP: 212.63.193.10
Probable Submitter: 83.190.139.162
----
Manual Page -- http://www.php.net/manual/en/function.strlen.php
Edit -- https://master.php.net/note/edit/77256
Del: integrated -- https://master.php.net/note/delete/77256/integrated
Del: useless -- https://master.php.net/note/delete/77256/useless
Del: bad code -- https://master.php.net/note/delete/77256/bad+code
Del: spam -- https://master.php.net/note/delete/77256/spam
Del: non-english -- https://master.php.net/note/delete/77256/non-english
Del: in docs -- https://master.php.net/note/delete/77256/in+docs
Del: other reasons-- https://master.php.net/note/delete/77256
Reject -- https://master.php.net/note/reject/77256
Search -- https://master.php.net/manage/user-notes.php