Simple Machines Community Forum

Customizing SMF => Tips and Tricks => Topic started by: 127.0.0.1 on September 05, 2004, 05:18:15 PM

Title: Trimming Extra Carriage Returns in Messages
Post by: 127.0.0.1 on September 05, 2004, 05:18:15 PM
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












Title: Re: Trimming Extra Carriage Returns in Messages
Post by: [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]
Title: Re: Trimming Extra Carriage Returns in Messages
Post by: 127.0.0.1 on September 05, 2004, 07:19:27 PM
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*
Title: Re: Trimming Extra Carriage Returns in Messages
Post by: [Unknown] on September 05, 2004, 07:24:58 PM
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]
Title: Re: Trimming Extra Carriage Returns in Messages
Post by: 127.0.0.1 on September 05, 2004, 07:29:49 PM
Success. You own.
Title: Re: Trimming Extra Carriage Returns in Messages
Post by: Anguz on September 11, 2004, 04:05:04 AM
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.
Title: Re: Trimming Extra Carriage Returns in Messages
Post by: CapriSkye on September 12, 2004, 09:07:34 PM
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?
Title: Re: Trimming Extra Carriage Returns in Messages
Post by: Anguz on September 12, 2004, 09:15:29 PM
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. ;)
Title: Re: Trimming Extra Carriage Returns in Messages
Post by: Anguz on November 15, 2004, 11:52:06 PM
http://mods.simplemachines.org/index.php?mod=52
Title: Re: Trimming Extra Carriage Returns in Messages
Post by: dracomiconia on February 21, 2006, 03:51:55 AM
What about 1.0.6?

Does it work in ir?
Title: Re: Trimming Extra Carriage Returns in Messages
Post by: pctwo on September 28, 2006, 11:22:10 AM
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.


Title: Re: Trimming Extra Carriage Returns in Messages
Post by: Biology Forums on September 15, 2013, 07:01:30 PM
Reviving this thread. What happened to this mod? It is still useful in today's forums.