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
https://forums.phpfreaks.com/topic/65864-limit-string-to-specific-number-of-lines/
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. :)

 

 

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? 

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..">';

 

 

 

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!

I was thinking maybe I could do explodes. 

 

$explodep = explode(<p>,$string);

$count = count(explodep);

str_replace (</p>,"",$explodep);

for ($explodep as $value) {

$explodep-br[] = explode(<br />,$value);

}

 

Would something like this work?

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.