News:

SMF 2.1.4 has been released! Take it for a spin! Read more.

Main Menu

Trimming Extra Carriage Returns in Messages

Started by 127.0.0.1, September 05, 2004, 05:18:15 PM

Previous topic - Next topic

127.0.0.1

I want to trim all the extra carriage returns at the end of a message in display.template.php and wherever messages are displayed.

Also, I'd like to trim them in post.php when the message is written to the sql database.

How would I go about doing this?

PS I think it would be a good idea to add the trimming of all the extra carriage returns at the end of a message into the SMF code for feature versions. :P

I hate when people press enter a gazillion times at the end of a message













[Unknown]

Just use something like this in preparsecode: (at the end.)

$message = preg_replace('(<br />)+$i', '', $message);

That should get rid of trailing line breaks ;).

-[Unknown]

127.0.0.1

#2
That seems to delete the entire message.

I added it after:

$message = str_replace('{<{', '[[', $message);

in Subs-Post.php as the last line of the preparsecode function.

I also used $message = trim($message, "\r"); but it only takes care of one carriage return  :-\

*searches google for a solution in the meantime*

[Unknown]

Quote from: [Unknown] on September 05, 2004, 05:46:39 PM
Just use something like this in preparsecode: (at the end.)

$message = preg_replace('(<br />)+$i', '', $message);

That should get rid of trailing line breaks ;).

-[Unknown]

Oops, sorry, bad typo..

$message = preg_replace('~(<br />)+$~i', '', $message);

-[Unknown]

127.0.0.1


Anguz

#5
Would that work if there's spaces or tabs mixed with the newlines?

In a YaBB SE forum I had, I used this
$message = trim(preg_replace("/\n[^\w]*\n/", "\n\n", $message));
before turning the newlines into breaks and worked very well. If I remember correctly, it worked with excess newlines in the middle of the message too (I had members that used several newlines between paragraphs). I guess I could've written it like this
$message = trim(preg_replace('~\n\s+\n~', "\n\n", $message));

I guess in SMF it'd work like this, in preparsecode (Sources/Subs-Post.php) look for
// Remove \r's, replace tabs with spaces, two spaces with hard spaces, and \n's with breaks. (last is optional...)


and right after add the line of code I gave above
$message = trim(preg_replace('~\n\s+\n~', "\n\n", $message));


That'll remove any whitespace (newline, space, tab, etc) from the beginning or end of the message and between two newlines in the middle of the message.

So this
Quote
     
abc

     

123

   
turns into this
Quoteabc

123

This'll affect code blocks too, so if you use those it may not be the best way to do it. I'm not sure how to write the regex to omit what's between code tags though. You'd have to forget about the excess newlines in the middle of the message, but you can still do it at the beginning and end. Just use trim
$message = trim($message);


If you want to apply this to the rest message that's not inside code tags too, you could do this in addition to the line with trim... look for
else
fixTags($parts[$i]);


and change it to
else
{
$parts[$i] = strtr($parts[$i], array('&nbsp; ' => '  '));
if ($breaks)
$parts[$i] = strtr($parts[$i], array('<br />' => "\n"));
$parts[$i] = preg_replace('~\n\s+\n~', '\n\n', $parts[$i]);
$parts[$i] = strtr($parts[$i], array('  ' => '&nbsp; '));
if ($breaks)
$parts[$i] = strtr($parts[$i], array("\n" => '<br />'));

fixTags($parts[$i]);
}


I don't like it very much because it's redundant having to turn all the &nbsp; and <br /> back into " " and "\n" and then back again, but it's the place where you're sure you won't affect code blocks.

I haven't tested any of this, but I think it works.
Cristián Lávaque http://cristianlavaque.com

CapriSkye

Quote

Oops, sorry, bad typo..

$message = preg_replace('~(<br />)+$~i', '', $message);

-[Unknown]

do we add that only in Sub-Post.php? how about display.template.php?

Anguz

If the message is fixed before storing it in the database, you don't need to worry about how it is when you get it back from it. Preparse deals with before saving it. It won't help with things that are already posted, though, but doing this when displaying will slow down the topic's display and those messages that are there like that will eventually be old and not seen anymore and all the new ones will be fixed. ;)
Cristián Lávaque http://cristianlavaque.com


dracomiconia


pctwo

works fine in 1.1rc3

I guess while we're at it, might as well trim leading CRs too

$message = preg_replace('~^(<br />)+~i', '', $message);

I also like to do this

$message = preg_replace('~\[/quote\](<br />)+~i', '[/quote]', $message);

to trim CRs after quote block, b/c some people will add CRs, some will not, and some add a whole bunch.  This makes it uniform.  I then set a margin-bottom: 1em for .quote in style.css.



Biology Forums

Reviving this thread. What happened to this mod? It is still useful in today's forums.

Advertisement: