это может работать, не проверялось, но должно работать и делать то, что вы просили ...
это первый способ - обрезать @ количество символов
<?php
function truncate_by_characters ( $s, $l = 45, $e = '...' )
{
$sl = strlen ( $s );
$ns = 0;
$cr = 0;
$rs = '';
preg_match_all ( '/<[^>]*>[^<]+<\/[^>]*>|<(?!\/)[^>]*>/', $s, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER );
foreach ( $m as $v )
{
if ( ( $v[0][1] - $ns ) >= $l )
{
break;
}
$ss = substr ( $s, $ns, ( $v[0][1] - $ns ) );
$cr += strlen ( $ss );
$rs .= $ss . $v[0][0];
$ns = ( $v[0][1] + strlen ( $v[0][0] ) );
}
if ( $cr < $l )
{
if ( ( $ns + ( $l - $cr ) ) > $sl )
{
$ts = substr ( $s, $ns, ( $sl - $ns ) );
}
else
{
$ts = substr ( $s, $ns, ( $l - $cr ) );
}
for ( $x = ( strlen ( $ts ) - 1 ); $x >= 0; $x -= 1 )
{
$z = array ( "\t", "\r", "\n", " ", "\0", "\x0B" );
if ( in_array ( $ts[$x], $z ) )
{
$rs .= substr ( $ts, 0, $x );
break;
}
}
}
return $rs . $e;
}
$truncate_text = 'This <img src="" alt=""> function works great however if a <a href="http://.com/page.html?test=1">html element</a> is found it will stop where the 45th character count is no matter what, which breaks the html element. How can I make an exception for this? I\'m guessing some kind of regex but not sure what is best in this case.';
//$truncate_text = 'This function works great however if a html element is found it will stop where the 45th character count is no matter what, which breaks the html element. How can I make an exception for this? I\'m guessing some kind of regex but not sure what is best in this case.';
$truncate_characters = 45;
$truncate_ending = '...';
echo truncate_by_characters ( $truncate_text, $truncate_characters, $truncate_ending );
?>
ПРИМЕЧАНИЕ.вышеуказанная функция работает, однако нижеприведенная функция не дает никаких результатов.
этот второй - усечь @ количество слов
<?php
function truncate_by_words ( $s, $l = 45, $e = '...' )
{
$sl = strlen ( $s );
$ns = 0;
$tw = 0;
$rs = '';
preg_match_all ( '/<[^>]*>[^<]+<\/[^>]*>|<(?!\/)[^>]*>/', $s, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER );
foreach ( $m as $v )
{
$ss = substr ( $s, $ns, ( $v[0][1] - $ns ) );
$wf = str_word_count ( $ss, 2 );
$wc = count ( $wf );
if ( ( $tw + $wc ) >= $l )
{
$mw = 1;
foreach ( $wf AS $wp => $wv )
{
if ( ( $tw + $mw++ ) == $l )
{
$ss = substr ( $s, $ns, ( $wp - $ns ) );
$rs .= $ss . $wv;
$ns = ( $wp + strlen ( $wv ) );
$tw = $l;
break;
}
}
}
$tw += $wc;
$rs .= $ss . $v[0][0];
$ns = ( $v[0][1] + strlen ( $v[0][0] ) );
}
if ( $tw < $l )
{
$ss = substr ( $s, $ns, ( $sl - $ns ) );
$wf = str_word_count ( $ss, 2 );
$wc = count ( $wf );
if ( ( $tw + $wc ) <= $l )
{
$rs .= $ss;
}
else
{
$mw = 1;
foreach ( $wf AS $wp => $wv )
{
if ( ( $tw + $mw++ ) == $l )
{
$ss = substr ( $ss, 0, $wp );
$rs .= $ss . $wv;
break;
}
}
}
}
return $rs . $e;
}
$truncate_text = 'This <img src="" alt=""> function works great however if a <a href="http://.com/page.html?test=1">html element</a> is found it will stop where the 45th character count is no matter what, which breaks the html element. How can I make an exception for this? I\'m guessing some kind of regex but not sure what is best in this case.';
//$truncate_text = 'This function works great however if a html element is found it will stop where the 45th character count is no matter what, which breaks the html element. How can I make an exception for this? I\'m guessing some kind of regex but not sure what is best in this case.';
$truncate_words = 35;
$truncate_ending = '...';
echo truncate_by_words ( $truncate_text, $truncate_words, $truncate_ending );
?>