NottaGuru Posted January 25, 2009 Share Posted January 25, 2009 I 'think' this can be done... I am creating a message board and need some serious help. The scenario... A user enters a message and I use the nl2br function to keep the formatting... Now the message is displayed... I use substr to limit the displayed text to 165 characters The problem... I have a height limit of 100 px each for displaying the last 3 messages but if a user enters 10 lines with just the letter 'a', that is only counted as 10 characters, but the message is 10 lines tall and breaks the layout. Is there a way to stop this? I tried using CSS to limit each 'Message' to 100px, but no luck. The solution... I dunno. Link to comment https://forums.phpfreaks.com/topic/142328-solved-removing-excessive-newlines-and-breaks/ Share on other sites More sharing options...
dropfaith Posted January 25, 2009 Share Posted January 25, 2009 I found this great implementation to remove blank (empty lines) from a string at PHP web site. function removeEmptyLines($string) { return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $string); } Link to comment https://forums.phpfreaks.com/topic/142328-solved-removing-excessive-newlines-and-breaks/#findComment-745776 Share on other sites More sharing options...
jonsjava Posted January 25, 2009 Share Posted January 25, 2009 lets say you are using 12px font. That gives you a maximum of 8 lines that a user can have before they are breaking the 100px height limit. With that known, you know their message cannot be more than 8 lines. I would gladly help you further, but to do so, I'd need to see how you are currently doing the calculation, so I can help you figure out how to do it better. Link to comment https://forums.phpfreaks.com/topic/142328-solved-removing-excessive-newlines-and-breaks/#findComment-745777 Share on other sites More sharing options...
NottaGuru Posted January 25, 2009 Author Share Posted January 25, 2009 I tried the example above both as the first code to process the displayed message and also as the last code. I need the message to be the original message for when a user reads the whole message. I need help with the displayed partial message. Here is the relevant code... Form submitted... $NewMessage=htmlentities($_POST[NewMessage]); $NewMessage=nl2br($NewMessage); If no errors (blank form, unacceptable words, etc), message entered into a mysql table. The display... Info drawn from the table and shown on the screen in a while loop because only showing last 3 messages. // 165 characters per message $Message=substr($messagerow[Message],0,165); if(strlen($messagerow[Message])>165) { $Message.="....."; } <p class='message'><a href='' title=''>$Message</a></p> The CSS... #message_board_container div.message { padding: 0 0 0 2px; width: 210px; height: 100px;} For the CSS, I've used height, max-height, and min-height and max-height both. When the user enters a message with 10 new lines of 1 character each, that is 10 characters and shown as 10 lines because it does not meet the 165 character length. What happens is the extra lines bleed down in to the next message. Link to comment https://forums.phpfreaks.com/topic/142328-solved-removing-excessive-newlines-and-breaks/#findComment-745985 Share on other sites More sharing options...
NottaGuru Posted January 25, 2009 Author Share Posted January 25, 2009 Found a solution... $Message=str_replace("<br />","",$Message); It removes ALL newlines, so I'm gonna display 165 chars max up to the first line break... Hey... It may not be pretty, but it works and does what I want it to do. Link to comment https://forums.phpfreaks.com/topic/142328-solved-removing-excessive-newlines-and-breaks/#findComment-746034 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.