Jump to content

Confining text from DB to 5 lines


smc

Recommended Posts

Well it's difficult because the length of a word or amount of characters can vary. I can say 5 words and have "okay okay okay okay okay" but two words like "superflouous language and vocabulary yay" which obviously will equal the same characters but much longer for the latter

You could use css to hide extra lines by using line height, em measurements and overflow: hidden

I'd agree if most of the items were about 5 lines in size and you just want to suppress the odd rogue that is longer. In this case, where the items are articles, then it is a waste of bandwidth to send 50 or more lines, say, only to have 45+ hidden.

Try this

 

<?php
$article = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nulla lacinia justo at velit. Nunc porttitor nisl porta mi. Curabitur sollicitudin purus vel felis. Maecenas nunc. Nulla facilisi. Integer vel ante sit amet ipsum consequat facilisis. Suspendisse hendrerit urna vel erat. Quisque auctor, erat nec ullamcorper sollicitudin, wisi leo hendrerit nisl, eu auctor turpis justo quis justo. Proin arcu metus, commodo quis, tincidunt varius, fermentum eget, lectus. Ut tellus. Sed nec dolor. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nam sed magna.";

// set the line length required, say 50 chars
$len = 50;

// break text every $len characters
$lines = explode("\n", wordwrap($article, $len));

// get first 5 lines and add <br/> tags
$toPrint = join('<br/>', array_slice($lines, 0, 5));

echo $toPrint;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.