note 113100 added to class.domnode
| From: | matej dot golian at gmail dot com | Date: | Thu, 29 Aug 2013 13:15:17 +0000 |
| Subject: | note 113100 added to class.domnode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-196120@lists.php.net to get a copy of this message | ||
Here is a little function that truncates a DomNode to a specified number of text characters. I use
it to generate HTML excerpts for my blog entries.
<?php
function makehtmlexcerpt(DomNode $html, $excerptlength)
{
$remove = 0;
$htmllength = strlen(html_entity_decode($html->textContent, ENT_QUOTES, 'UTF-8'));
$truncate = $htmllength - $excerptlength;
if($htmllength > $excerptlength)
{
if($html->hasChildNodes())
{
$children = $html->childNodes;
for($counter = 0; $counter < $children->length; $counter ++)
{
$child = $children->item($children->length - ($counter + 1));
$childlength = strlen(html_entity_decode($child->textContent, ENT_QUOTES, 'UTF-8'));
if($childlength <= $truncate)
{
$remove ++;
$truncate = $truncate - $childlength;
}
else
{
$child = makehtmlexcerpt($child, $childlength - $truncate);
break;
}
}
if($remove != 0)
{
for($counter = 0; $counter < $remove; $counter ++)
{
$html->removeChild($html->lastChild);
}
}
}
else
{
if($html->nodeName == '#text')
{
$html->nodeValue = substr(html_entity_decode($html->nodeValue, ENT_QUOTES, 'UTF-8'),
0, $htmllength - $truncate);
}
}
}
return $html;
}
?>
----
Server IP: 69.147.83.199
Probable Submitter: 212.247.61.208
----
Manual Page -- http://php.net/manual/en/class.domnode.php
Edit -- https://master.php.net/note/edit/113100
Del: integrated -- https://master.php.net/note/delete/113100/integrated
Del: useless -- https://master.php.net/note/delete/113100/useless
Del: bad code -- https://master.php.net/note/delete/113100/bad+code
Del: spam -- https://master.php.net/note/delete/113100/spam
Del: non-english -- https://master.php.net/note/delete/113100/non-english
Del: in docs -- https://master.php.net/note/delete/113100/in+docs
Del: other reasons-- https://master.php.net/note/delete/113100
Reject -- https://master.php.net/note/reject/113100
Search -- https://master.php.net/manage/user-notes.php