Advertisement:

Author Topic: Trimming Extra Carriage Returns in Messages  (Read 27749 times)

Offline 127.0.0.1

  • Jr. Member
  • **
  • Posts: 265
  • Gender: Male
Trimming Extra Carriage Returns in Messages
« 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













Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: Trimming Extra Carriage Returns in Messages
« Reply #1 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]

Offline 127.0.0.1

  • Jr. Member
  • **
  • Posts: 265
  • Gender: Male
Re: Trimming Extra Carriage Returns in Messages
« Reply #2 on: September 05, 2004, 07:19:27 PM »
That seems to delete the entire message.

I added it after:
Code: [Select]
$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*
« Last Edit: September 05, 2004, 07:24:10 PM by 127.0.0.1 »

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: Trimming Extra Carriage Returns in Messages
« Reply #3 on: September 05, 2004, 07:24:58 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]

Offline 127.0.0.1

  • Jr. Member
  • **
  • Posts: 265
  • Gender: Male
Re: Trimming Extra Carriage Returns in Messages
« Reply #4 on: September 05, 2004, 07:29:49 PM »
Success. You own.

Offline Anguz

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 3,430
  • Gender: Male
    • cristianlavaque.com
Re: Trimming Extra Carriage Returns in Messages
« Reply #5 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
Code: [Select]
$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
Code: [Select]
$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
Code: [Select]
// 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
Code: [Select]
$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
Quote
abc

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
Code: [Select]
$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
Code: [Select]
else
fixTags($parts[$i]);

and change it to
Code: [Select]
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.
« Last Edit: September 11, 2004, 05:05:44 AM by Anguz »
Cristián Lávaque http://cristianlavaque.com

Offline CapriSkye

  • Full Member
  • ***
  • Posts: 574
    • Open
Re: Trimming Extra Carriage Returns in Messages
« Reply #6 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?

Offline Anguz

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 3,430
  • Gender: Male
    • cristianlavaque.com
Re: Trimming Extra Carriage Returns in Messages
« Reply #7 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. ;)
Cristián Lávaque http://cristianlavaque.com

Offline Anguz

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 3,430
  • Gender: Male
    • cristianlavaque.com
Cristián Lávaque http://cristianlavaque.com

Offline dracomiconia

  • Jr. Member
  • **
  • Posts: 300
    • Dracomiconia.com
Re: Trimming Extra Carriage Returns in Messages
« Reply #9 on: February 21, 2006, 03:51:55 AM »
What about 1.0.6?

Does it work in ir?

Offline pctwo

  • Semi-Newbie
  • *
  • Posts: 68
Re: Trimming Extra Carriage Returns in Messages
« Reply #10 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

Code: [Select]
$message = preg_replace('~^(<br />)+~i', '', $message);
I also like to do this

Code: [Select]
$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.



Offline Study Force

  • SMF Hero
  • ******
  • Posts: 3,101
    • @studyforceps on Twitter
    • Study Force
Re: Trimming Extra Carriage Returns in Messages
« Reply #11 on: September 15, 2013, 07:01:30 PM »
Reviving this thread. What happened to this mod? It is still useful in today's forums.