Jump to content

Confining text from DB to 5 lines


smc

Recommended Posts

Hey everyone,

 

Basically I have an article in a Database and I would like it to display in an HTML table but I only want it to show up to 5 lines. I'm not sure how I can accomplish this.

 

Any help would be greatly appreciated!

 

Thanks!!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.