Simple Machines Community Forum

Customizing SMF => Tips and Tricks => Thema gestartet von: 127.0.0.1 in September 05, 2004, 05:18:15 NACHMITTAGS

Titel: Trimming Extra Carriage Returns in Messages
Beitrag von: 127.0.0.1 in September 05, 2004, 05:18:15 NACHMITTAGS
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












Titel: Re: Trimming Extra Carriage Returns in Messages
Beitrag von: [Unknown] in September 05, 2004, 05:46:39 NACHMITTAGS
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]
Titel: Re: Trimming Extra Carriage Returns in Messages
Beitrag von: 127.0.0.1 in September 05, 2004, 07:19:27 NACHMITTAGS
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*
Titel: Re: Trimming Extra Carriage Returns in Messages
Beitrag von: [Unknown] in September 05, 2004, 07:24:58 NACHMITTAGS
Zitat von: [Unknown] in September 05, 2004, 05:46:39 NACHMITTAGS
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]
Titel: Re: Trimming Extra Carriage Returns in Messages
Beitrag von: 127.0.0.1 in September 05, 2004, 07:29:49 NACHMITTAGS
Success. You own.
Titel: Re: Trimming Extra Carriage Returns in Messages
Beitrag von: Anguz in September 11, 2004, 04:05:04 VORMITTAG
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
Zitat
     
abc

     

123

   
turns into this
Zitatabc

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.
Titel: Re: Trimming Extra Carriage Returns in Messages
Beitrag von: CapriSkye in September 12, 2004, 09:07:34 NACHMITTAGS
Zitat

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?
Titel: Re: Trimming Extra Carriage Returns in Messages
Beitrag von: Anguz in September 12, 2004, 09:15:29 NACHMITTAGS
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. ;)
Titel: Re: Trimming Extra Carriage Returns in Messages
Beitrag von: Anguz in November 15, 2004, 11:52:06 NACHMITTAGS
http://mods.simplemachines.org/index.php?mod=52
Titel: Re: Trimming Extra Carriage Returns in Messages
Beitrag von: dracomiconia in Februar 21, 2006, 03:51:55 VORMITTAG
What about 1.0.6?

Does it work in ir?
Titel: Re: Trimming Extra Carriage Returns in Messages
Beitrag von: pctwo in September 28, 2006, 11:22:10 VORMITTAG
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.


Titel: Re: Trimming Extra Carriage Returns in Messages
Beitrag von: Biology Forums in September 15, 2013, 07:01:30 NACHMITTAGS
Reviving this thread. What happened to this mod? It is still useful in today's forums.