Anti-Moronic Posted February 13, 2010 Share Posted February 13, 2010 Hi, I have a problem at the moment where some problem users are taking advantage of the new line in the text area...they are submitting text with 100 new lines and obviously the page is stretching forever. Is there any way of using nl2br but only converting a certain amount of new lines? like only the first 10? Appreciate any insight. Quote Link to comment Share on other sites More sharing options...
jskywalker Posted February 13, 2010 Share Posted February 13, 2010 http://www.php.net/manual/en/function.str-replace.php is your answer... $textarea = str_replace('<br>','', $textarea); Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 13, 2010 Share Posted February 13, 2010 That will get rid of them all - the OP wants to keep some Quote Link to comment Share on other sites More sharing options...
premiso Posted February 13, 2010 Share Posted February 13, 2010 Here is one way to do it, basically if 10 blank lines are reached nothing is appended to the string until that variable is reset (meaning non-empty data was output). <?php $string = "somtext\r\nmore text\r\n\r\n\r\n\r\nmore text\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nmoretext here\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"; $strArr = explode("\r\n", $string); $string = ""; $i=1; foreach ($strArr as $str) { if (!empty($str)) { $string .= $str; // reset $i $i=1; }else { if ($i < 10) { $string .= "\r\n"; $i++; } } } echo nl2br($string) . "end"; ?> Quote Link to comment Share on other sites More sharing options...
jskywalker Posted February 13, 2010 Share Posted February 13, 2010 ok, if you want to keep some than do http://www.php.net/manual/en/function.preg-replace.php $textarea = preg_replace("/(<br>)*/","<br>",$textarea); this will replace multiple '<br>', leaving maximal one '<br>' Quote Link to comment Share on other sites More sharing options...
Mchl Posted February 13, 2010 Share Posted February 13, 2010 Wouldn't it be better to replace newlines before nl2br is applied? This way you can switch between HTML/XHTML mode of nl2br without modyfing regex. Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted February 13, 2010 Author Share Posted February 13, 2010 Thanks guys appreciate the help. I should clarify too. What I meant was to only parse up to a number of new lines in total. Thanks premiso, that will help if users persist on new lines with no data in between. I'm thinking what would be ideal is to use preg_replace with an offset and remove any \r\n after the limit (say 10). Then I can safely use nl2br as needed. Thanks again. Quote Link to comment Share on other sites More sharing options...
Mchl Posted February 13, 2010 Share Posted February 13, 2010 I'll move it to regex forum, as it's more likely someone will notice it there. Quote Link to comment Share on other sites More sharing options...
Mchl Posted February 13, 2010 Share Posted February 13, 2010 I'm not really good with regex, but how about something like: preg_replace('#${10,}#',"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n",$textarea); It can probably be written better, but should do the job... I think... [edit] Oh well... compilation error... [edit] Silly me. preg_replace('#(\r\n|\r|\n){10,}#',"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n",$textarea); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.