How to trim text in PHP while keeping a required number of lines (limiting the number of line breaks)
function TrimObjectLines($text, $NumSymInLine, $numMaxLine)
{
$CountLines = 0;
$textresult = '';
$len = mb_strlen($text, 'UTF-8');
if ($len > 0) $CountLines = 1;
$entry = (str_word_count($text, 2, "АаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя"));
$leng = 0;
foreach ($entry as $k => $v)
{
$leng = $leng + mb_strlen($v);
if ($CountLines < $numMaxLine) $textresult .= ' ' . $v;
if ($leng > $NumSymInLine)
{
$CountLines++;
$leng = $k;
}
}
return $textresult.'...';
}
if you just need to find out the number of lines the text will be split into, that's written here
http://my-city.com.ua/forum/topic.php?f=kak-na-php-uznat-kolichetvo-strok-v-bloke-s-zadannojj-shirinojj-to-est-opredelit-chislo-perenosov&forum=38&topic=70
Comments