Jump to content

Limit number of \n ?


immanuelx2

Recommended Posts

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..

Link to comment
Share on other sites

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", '*');

Link to comment
Share on other sites

  • 2 weeks later...

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);

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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:

 

regexp1.jpg

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.