Prodigal Son Posted October 18, 2008 Share Posted October 18, 2008 I'm currently using the nl2br function to create break lines for new lines from my db. How would I go about limiting the line breaks to max two in a row? For example, if I had some text that had 5 line breaks, it would get reduced to two only. Link to comment https://forums.phpfreaks.com/topic/129003-solved-nl2br-and-multiple-line-breaks/ Share on other sites More sharing options...
GingerRobot Posted October 18, 2008 Share Posted October 18, 2008 Regex? <?php $str = "Test \n Test \n\n Test \n\n\n\n\n Test"; $str = preg_replace("|(\\n){3,}|s","\n\n",$str); echo nl2br($str); ?> Link to comment https://forums.phpfreaks.com/topic/129003-solved-nl2br-and-multiple-line-breaks/#findComment-668768 Share on other sites More sharing options...
Prodigal Son Posted October 19, 2008 Author Share Posted October 19, 2008 Hmm... weird. I used this to prevent user input from creating big spaces and I tried this and it worked nicely preventing multiple line breaks. But for some reason a user was still able to create more than 2 line breaks. I tried copying and pasting their message with lots of line breaks, but when I did it, it got cut down to 2... so I have no idea how that individual did that. Anyone see why the individual could do that? Link to comment https://forums.phpfreaks.com/topic/129003-solved-nl2br-and-multiple-line-breaks/#findComment-669263 Share on other sites More sharing options...
DarkWater Posted October 19, 2008 Share Posted October 19, 2008 Either their post was already IN the database...or you don't use htmlspecialchars() and they just used a straight <br /> tag. Link to comment https://forums.phpfreaks.com/topic/129003-solved-nl2br-and-multiple-line-breaks/#findComment-669264 Share on other sites More sharing options...
Prodigal Son Posted October 19, 2008 Author Share Posted October 19, 2008 Either their post was already IN the database...or you don't use htmlspecialchars() and they just used a straight <br /> tag. Hmm, nope their post wasn't in the database yet. I do use htmlspecialchars so the br tag doesn't work. So I find it weird how they did that since I even copy and pasted their input and mine got reduced. Link to comment https://forums.phpfreaks.com/topic/129003-solved-nl2br-and-multiple-line-breaks/#findComment-669433 Share on other sites More sharing options...
Prodigal Son Posted October 19, 2008 Author Share Posted October 19, 2008 Either their post was already IN the database...or you don't use htmlspecialchars() and they just used a straight <br /> tag. After playing around for a bit I seem to have found out how they did it. For each space character you make after pressing enter it creates a breakline. For example if you type Hello followed by the enter key 5 times and Goodbye, you'd get Hello, 2 breaklines, then Goodbye. But if you type Hello, press enter, press space, press enter, press space, press enter, press enter, and type goodbye. You can get 3 line gaps in between. Haven't used regex in a long time, and I was never that good with it to begin with, but I tried this: $str = preg_replace("/(\\s){2,}/s"," ",$str); $str = preg_replace("/(\\n){3,}/s","\n\n",$str); But it just ended up removing all breaklines and extra spaces instead. Anyone see what's wrong? Link to comment https://forums.phpfreaks.com/topic/129003-solved-nl2br-and-multiple-line-breaks/#findComment-669580 Share on other sites More sharing options...
GingerRobot Posted October 20, 2008 Share Posted October 20, 2008 Try: <?php $str = "Test \n Test \n\n\n\n\n Test \n \n \n \n \n Test"; $str = preg_replace("|(\\n\s*){3,}|s","\n\n",$str); echo nl2br($str); ?> Link to comment https://forums.phpfreaks.com/topic/129003-solved-nl2br-and-multiple-line-breaks/#findComment-669807 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.