Forum Moderators: coopster

Message Too Old, No Replies

too many new lines

how to get rid of...

         

Anguz

8:00 am on Aug 2, 2003 (gmt 0)

10+ Year Member



I have a member in my forums that posts a line, hits enter 3 times, another line, enter 3 times.. in the end, the message is 4-5 times longer than needed

and now other members are doing the same... -_-

what function could I use to get rid of the extra line breaks and leave just one?

MonkeeSage

8:22 am on Aug 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Anguz:

I'm not sure as to how you would go about it in PHP, but I think the basic idea would be to replace '\n\n\n' in their posts with '\n'.

Jordan

vincevincevince

8:37 am on Aug 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$message=preg_replace("/\n\n/","\n",$message);

and for when they get a bit more cunning:

$message=preg_replace("/\n[^\w]*\n/","\n",$message);

Anguz

1:34 pm on Aug 2, 2003 (gmt 0)

10+ Year Member



hmm... yeah, I think regex is the answer, cause sometimes he puts two /n, sometimes five...

what does the last regex do? how would your write it to fix something like:

-------------------------------------
this is a text

another line


another line

another line

-------------------------------------

thank you for your help! :)

Anguz

1:38 pm on Aug 2, 2003 (gmt 0)

10+ Year Member



the example didn't work, this forum took out many repeated /n lol

basically, I need to repeat any set of multiple (two or more) /n together, with a single one, anywhere in the text

saoi_jp

3:19 pm on Aug 5, 2003 (gmt 0)

10+ Year Member



Here's part of a function I use in a forum. I added comments, and left only the parts that are relevant to your question. (In other words, I removed addslashes(), etc.)

function scrub_da_post($content)
{
// turn returns to newlines:
$content = str_replace("\r", "\n", $content);
// turn tabs to spaces:
$content = str_replace("\t", " ", $content);
// next is searching for double spaces.
while(preg_match("/ /i", "$content"))
{
// replace them with single spaces:
$content = str_replace(" ", " ", $content);
}
// looks for spaces after a newline:
while(preg_match("/\n /", "$content"))
{
// remove that space:
$content = str_replace("\n ", "\n", $content);
}
// look for two newlines:
while(preg_match("/\n\n/i", "$content"))
{ // turn it to one newline
$content = str_replace("\n\n", "\n", $content);
}
// the \n now separates paragraphs; change \n to <p>:
$content = "<p>" . str_replace("\n", "</p><p>", $content) . "</p>";
$content = str_replace("<p></p>", "", $content);
// done!
return $content;
}

vincevincevince

5:51 pm on Aug 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$message=preg_replace("/\n[^\w]*\n/","\n",$message);

what does the last regex do?

It finds any two newlines and replaces them for one, ignoring anything in between them which is not a word charecter. for example \nSo\n wouldn't get caught by \n \n or \n\t\n would get caught - eg, it ignores whitespace

Anguz

6:46 pm on Aug 5, 2003 (gmt 0)

10+ Year Member



saoi_jp:
thx for that code :)

vincevincevince:
so if I have "\n\n \n \n", it'll be replaced with "\n"? if so, that's exactly what I need ;)

vincevincevince

6:49 pm on Aug 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Anguz ...


<?php
$message="A\n \n\t\nB\n\nC\n\n\nD\n\n \n\nE";
echo "OLD: ".nl2br($message)."<hr>";
$message=preg_replace("/\n[^\w]*\n/","\n",$message);
echo "NEW: ".nl2br($message);
?>

Output:


OLD: A


B

C


D



E
--------------------------------------------------------------------------------
NEW: A
B
C
D
E

Anguz

8:47 pm on Aug 5, 2003 (gmt 0)

10+ Year Member



great!

although I just noticed something... it won't be possible to have paragraphs is it's like this...

maybe it should let it have \n\n or everything will be just one block of text...

then the change should be that if there's over \n\n, change it to that... if there's two or one, just leave it alone

how would the regex be changed to do this? ^^;;

Anguz

9:16 pm on Aug 5, 2003 (gmt 0)

10+ Year Member



would preg_replace("/\n[^\w]*\n/","\n\n",$message) work? i added an extra \n there

vincevincevince

10:02 pm on Aug 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd go for this one rather than yours, but yours is nice too :-)

$message=preg_replace("/\n[^\w]*\n[^\w]*\n/","\n",$message);