<?php
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com
function AbstractHTMLContents($html, $maxLength=100){
mb_internal_encoding("UTF-8");
$printedLength = 0;
$position = 0;
$tags = array();
$newContent = '';
$html = $content = preg_replace("/<img[^>]+\>/i", "", $html);
while ($printedLength < $maxLength && preg_match('{</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;}', $html, $match, PREG_OFFSET_CAPTURE, $position))
{
list($tag, $tagPosition) = $match[0];
// Print text leading up to the tag.
$str = mb_strcut($html, $position, $tagPosition - $position);
if ($printedLength + mb_strlen($str) > $maxLength){
$newstr = mb_strcut($str, 0, $maxLength - $printedLength);
$newstr = preg_replace('~\s+\S+$~', '', $newstr);
$newContent .= $newstr;
$printedLength = $maxLength;
break;
}
$newContent .= $str;
$printedLength += mb_strlen($str);
if ($tag[0] == '&') {
// Handle the entity.
$newContent .= $tag;
$printedLength++;
} else {
// Handle the tag.
$tagName = $match[1][0];
if ($tag[1] == '/') {
// This is a closing tag.
$openingTag = array_pop($tags);
assert($openingTag == $tagName); // check that tags are properly nested.
$newContent .= $tag;
} else if ($tag[mb_strlen($tag) - 2] == '/'){
// Self-closing tag.
$newContent .= $tag;
} else {
// Opening tag.
$newContent .= $tag;
$tags[] = $tagName;
}
}
// Continue after the tag.
$position = $tagPosition + mb_strlen($tag);
}
// Print any remaining text.
if ($printedLength < $maxLength && $position < mb_strlen($html))
{
$newstr = mb_strcut($html, $position, $maxLength - $printedLength);
$newstr = preg_replace('~\s+\S+$~', '', $newstr);
$newContent .= $newstr;
}
// Close any open tags.
while (!empty($tags))
{
$newContent .= sprintf('</%s>', array_pop($tags));
}
return $newContent;
}
$sText = '<p>Ключевым элементом в области снижения влияния объектов производства на водные ресурсы является уменьшение забора воды и качество <i>очистительных</i> процедур.</p>
<p>Для <strong>этих</strong> целей Группа «Газпром» проводит организационно-технические мероприятия, <img src="http://osb.loc/images/logo.jpg" alt="test" text> в частности по установке средств измерения расхода воды, а также установке и наладке запорно-затворной арматуры на своих предприятиях, что способствует существенной экономии водных ресурсов.</p>
<p>Повышение эффективности действующих очистных сооружений и внедрение новых технологий очистки стоков позволяет обеспечить нормативное качество очистки сточных вод. Общая мощность очистных сооружений «Газпрома» в 2010 г. составила 153,263 млн куб. м. Половина нормативно очищенных стоков прошла очистку на сооружениях биологической очистки, 39% — механической очистки, 12% — физико-химической очистки сточных вод.</p>';
//echo String::truncate( $sText, 150, ' ...'); //jo<...
//echo "\n\n\n";
echo AbstractHTMLContents( $sText, 30, ' ...', true); //jo<i><b>n</b></i>...
//echo "\n\n\n";
//echo String::truncate( $sText, 150, ' ...', false); //jo<i><b>n...
- Output for git.master, git.master_jit, rfc.property-hooks
- <p>Ключевым</p>
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches
Archived branches
Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:
72.47 ms | 401 KiB | 8 Q