Jump to content

Limit String To Specific Number Of Lines


fanfavorite

Recommended Posts

I was wondering how I would go about setting the number of lines for a string.  For instance, if I have:

 

This<br />Is<br />Just<br />A<br />Test<br />

 

Say I just have room for 3 lines, I want it to say on the page:

 

This

Is

Just

- click here to read the entire string

 

The reason for this is I want to create a news section on the main page, that has the brief story and then click the link to go to the full story.

 

Any help is appreciated.  Thanks!

 

-JC

Link to comment
Share on other sites

Hello fanfavorite,

If your text did not have line breaks, and were wrapped around to new lines based on your html content holder (that may vary from browser to browser), there might be some issues.

 

If there are explicit line breaks in your content using '\n' like what is in your post, I would count the line breaks like this:

 

<?php
$content = "Hello. 
This is some 
kick butt 
content!";

$splitContent = split("\n",$content);
$shortContent = ""; //new shortened content
$lineInt = 0;

while($lineInt < 3) {
   $shortContent .= $shortContent[$lineInt] . "\n";
   $lineInt++;
}

?>

 

If I typed that correctly, it should work as a start. Hope your project works out well. :)

 

 

Link to comment
Share on other sites

That was what I was afraid of.  The text is dynamically entered and allows full html.  Would I have to do something like this:

 

$string = "<p>This<br />Is<br />Just<br />A<br />Test</p>"
$maxperline = 50;
$maxlines = 3;

 

- Figure out how many characters are displayed between each paragraph, list item or line break

- If larger than 50, add 1 line for each set

- For each paragraph, line break or list item add 1 line

- Once get to maximum number of lines, display just those

 

Or is there an easier route? 

Link to comment
Share on other sites

I wouldn't deal with new lines  at all.  strip them out and put your text in a div tag that has a set width.  let the browser do the wrapping

$maxChar = 10;
$string = "hello
world
there is a lot more text then i want here";
$trailer = "...";

$newString = ereg_replace("\n","",$string); // strip new lines
$newString = ereg_replace("<.*[bB][rR].*>","",$newString); // Strip Breaks

// Aquire New String to display
$dispString = substr($newString,0,$maxChar);
// Append "..." suffix if nessisary.
if(strlen($newString)>$maxChar))$dispString .= $trailer;

echo $dispString;

 

Alternatively, you could define your $trailer to be something like

$trailer='<a href="http://www... etc..">';

 

 

 

Link to comment
Share on other sites

Thank you both for your help.  I tried your solution lead, but the ereg_replace would not work for me.  Seemed to erase the entire string.  I think I will start with strip_tags as a base and go from there.  I have to have the html formatting in the final output.  So I think I will have to mark where all the paragraphs, line breaks and bullets are and create some kind of formula to determine the space it will take up. 

 

If anyone has any options to keep the formatting, please let me know.  Thanks!

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.