You get a bonus - 1 coin for daily activity. Now you have 1 coin

How to trim text in PHP while keeping a required number of lines (limiting the number of line breaks)

Practice




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

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Running server side scripts using PHP as an example (LAMP)"

Terms: Running server side scripts using PHP as an example (LAMP)