Jeśli interesuje Cię obcięcie stringu do X "faktycznych" znaków, ale włącznie z tagami html to:
Wykorzystując ten skrypt:
http://snipplr.com/view/3618/function closetags ( $html )
{
#put all opened tags into an array
$openedtags = $result[1];
#put all closed tags into an array
$closedtags = $result[1];
$len_opened = count ( $openedtags );
# all tags are closed
if( count ( $closedtags ) == $len_opened )
{
return $html;
}
# close tags
for( $i = 0; $i < $len_opened; $i++ )
{
if ( !in_array ( $openedtags[$i], $closedtags ) )
{
$html .= "</" . $openedtags[$i] . ">";
}
else
{
}
}
return $html;
}
// Do powyższej funkcji z podanej strony dodajemy jeszcze funkcję:
function skracaj($str, $max=500) {
$ret = '';
$inTag = false;
$len = 0;
for($i = 0; $i < iconv_strlen($str, 'UTF-8'); $i++){
$char = iconv_substr($str, $i, 1, 'UTF-8');
if($inTag == false && $char !== '<') {
$ret .= $char;
$len++;
} else if(!$inTag && $char == '<') {
$inTag = true;
} else if($char == '>') {
$inTag = false;
}
if(!$inTag && $len >= $max) return $ret;
}
return $ret;
}
a używamy tego:
$str = "<div>Jakiś test jakiś tekst jakiś tekst <strong><em>Test!</em> TEEEEKST</strong> TEEEEKST <em>";
$str .= "TEEEEKST</em> TEEEEKST</div>";
echo closetags
( skracaj
($str, 40
) );
PS. Piszę w utf8, aby to zadziałało w iso8859-2 trzebaby było trochę zmienić