immanuelx2 Posted May 26, 2009 Share Posted May 26, 2009 Hey all. I am trying to implement a comment system for my website, which makes use of nl2br(). My question is how to limit the number of consecutive \n so that users cannot spam enter and enter a long blank comment. I would preferably like to replace all consecutive \n with just "" (nothing) after the first two ( \n\n ) Regex Noob here, so please explain it for a dummy Thanks in advance.. Quote Link to comment https://forums.phpfreaks.com/topic/159749-limit-number-of-n/ Share on other sites More sharing options...
Ken2k7 Posted May 26, 2009 Share Posted May 26, 2009 <?php $str = "some value...\n\n\n\n\n\n...some more value"; $str = preg_replace("#\n{3,}#","\n\n",$str); Quote Link to comment https://forums.phpfreaks.com/topic/159749-limit-number-of-n/#findComment-842597 Share on other sites More sharing options...
immanuelx2 Posted May 26, 2009 Author Share Posted May 26, 2009 not sure it's working Quote Link to comment https://forums.phpfreaks.com/topic/159749-limit-number-of-n/#findComment-842777 Share on other sites More sharing options...
nrg_alpha Posted May 27, 2009 Share Posted May 27, 2009 not sure it's working You can simply right-click and view source to see how many blank lines between text (there should not be more than 1, as that lone blank line represents the first \n, while the text on the line after that starts off with the other). If you're not comfortable with that (or not convinced or what have you), you can simply throw in a simple silly line of code (temporarily for debugging of course) to convert \n to another character and have a look from there: $str = strtr($str, "\n", '*'); Quote Link to comment https://forums.phpfreaks.com/topic/159749-limit-number-of-n/#findComment-843328 Share on other sites More sharing options...
immanuelx2 Posted June 4, 2009 Author Share Posted June 4, 2009 Any idea why this isn't working? Different but similar problem.. I'm basically trying to ignore the first \n (newline) after the tag <?php $bbcode = array ( '@\[/quote\]\\n@si' => '[/quote]', // this one doesn't work '@\[quote=(.*?)\](.*?)\[/quote\]@si' => '<div class="quote"><h1>\\1 said:</h1>\\2</div>' // this one works fine ); $comment = preg_replace(array_keys($bbcode), array_values($bbcode), $comment); Quote Link to comment https://forums.phpfreaks.com/topic/159749-limit-number-of-n/#findComment-849627 Share on other sites More sharing options...
nrg_alpha Posted June 5, 2009 Share Posted June 5, 2009 Hmm, here is what I came up with: Example: $comment = <<<EOF Some text [quote] more text [/quote]\n Yet some more text [quote author=immanuelx2 link=topic=253981.msg1200517#msg1200517 date=1244156940]I love regex![/quote] EOF; $bbcode = array('#\[/quote\]\n#i'=>'[/quote]', '#\[quote author=\s*(\w+)[^\]]+\](.*?)\[/quote\]#is'=>'<div class="quote"><h1>\1 said:</h1>\2</div>'); $comment = preg_replace(array_keys($bbcode), array_values($bbcode), $comment); echo $comment; Output: Some text [quote] more text [/quote] Yet some more text <div class="quote"><h1>immanuelx2 said:</h1>I love regex!</div> You don't need to use double escapes for \n in your initial \[/quote\]\\n@si nor is the s modifier needed, as that only applies to the dot character. Since there is no dot in that pattern, you don't need it. As for the second part, just note that a web page should only have a single <h1> tag (which is sort of like a headline of an article... it has a lot of weight in search engines).. so I wouldn't recommend using that tag. I am assuming you wanted to get the author's name, so I made some changes to match just the author instead of doing what you have done which is get anything inside the tag. If your system is anything like this one, there is more than just the author's name (as you can see from the fake $comment code in the above snippet. Quote Link to comment https://forums.phpfreaks.com/topic/159749-limit-number-of-n/#findComment-849669 Share on other sites More sharing options...
immanuelx2 Posted June 5, 2009 Author Share Posted June 5, 2009 '#\[/quote\]\n#i'=>'[/quote]' still aint working Quote Link to comment https://forums.phpfreaks.com/topic/159749-limit-number-of-n/#findComment-849685 Share on other sites More sharing options...
nrg_alpha Posted June 5, 2009 Share Posted June 5, 2009 It does work.. when echoing out $comment, try this: echo str_replace("\n", '*', $comment); You should not see the asterisk next to [/quote] Quote Link to comment https://forums.phpfreaks.com/topic/159749-limit-number-of-n/#findComment-849723 Share on other sites More sharing options...
immanuelx2 Posted June 5, 2009 Author Share Posted June 5, 2009 Here's what i've got: <?php function comment_output($comment) { $bbcode = array ( '#\[/quote\]\n#i' => '[/quote]', '@\[quote=(.*?)\](.*?)\[/quote\]@si' => '<div class="quote"><h1>\\1 said:</h1>\\2</div>' ); $comment = preg_replace(array_keys($bbcode), array_values($bbcode), $comment); $comment = nl2br($comment); return $comment; } Here's what it's giving me: Quote Link to comment https://forums.phpfreaks.com/topic/159749-limit-number-of-n/#findComment-850086 Share on other sites More sharing options...
nrg_alpha Posted June 5, 2009 Share Posted June 5, 2009 The fact that the \n is visible on screen (not talking about the right hand source code) denotes it is not a newline, but rather the literal \ and n, as newlines (along with spaces, tabs, return carriages, etc...) are white spaces... they should not be visible. Besides, if that was a true newline, then nl2br would convert it to <br />, which clearly it isn't. If you are referring to the <br /> in highlighted blue in the source code shot, if that was a true newline prior to nl2br conversion, it would only be removed if it followed right after [/quote]. So without seeing what the comment looked like before parsing, it's hard to say... Quote Link to comment https://forums.phpfreaks.com/topic/159749-limit-number-of-n/#findComment-850239 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.