fanfavorite Posted August 20, 2007 Share Posted August 20, 2007 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 More sharing options...
chocopi Posted August 20, 2007 Share Posted August 20, 2007 I think you would have to do words or chars instead of lines Link to comment https://forums.phpfreaks.com/topic/65864-limit-string-to-specific-number-of-lines/#findComment-329183 Share on other sites More sharing options...
ultrus Posted August 20, 2007 Share Posted August 20, 2007 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 https://forums.phpfreaks.com/topic/65864-limit-string-to-specific-number-of-lines/#findComment-329194 Share on other sites More sharing options...
fanfavorite Posted August 20, 2007 Author Share Posted August 20, 2007 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 https://forums.phpfreaks.com/topic/65864-limit-string-to-specific-number-of-lines/#findComment-329233 Share on other sites More sharing options...
chocopi Posted August 20, 2007 Share Posted August 20, 2007 you would should do strip_tags and then work out the stuff otherwise it will be less than you think Link to comment https://forums.phpfreaks.com/topic/65864-limit-string-to-specific-number-of-lines/#findComment-329252 Share on other sites More sharing options...
lead2gold Posted August 20, 2007 Share Posted August 20, 2007 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 https://forums.phpfreaks.com/topic/65864-limit-string-to-specific-number-of-lines/#findComment-329261 Share on other sites More sharing options...
fanfavorite Posted August 21, 2007 Author Share Posted August 21, 2007 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 https://forums.phpfreaks.com/topic/65864-limit-string-to-specific-number-of-lines/#findComment-329922 Share on other sites More sharing options...
fanfavorite Posted August 21, 2007 Author Share Posted August 21, 2007 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? Link to comment https://forums.phpfreaks.com/topic/65864-limit-string-to-specific-number-of-lines/#findComment-329936 Share on other sites More sharing options...
chocopi Posted August 22, 2007 Share Posted August 22, 2007 if you use htmlentities that will normal work by letting you display html code as text Link to comment https://forums.phpfreaks.com/topic/65864-limit-string-to-specific-number-of-lines/#findComment-330750 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.