Advertisement:

Author Topic: Removing extra line breaks from messages  (Read 3272 times)

Offline GL700Wing

  • Full Member
  • ***
  • Posts: 441
  • Gender: Female
Removing extra line breaks from messages
« on: January 21, 2018, 05:33:02 AM »
I've noticed members sometimes add extra line breaks at the end of their message (and very occasionally at the beginning) and if the message is subsequently quoted the extra line breaks are included in the quoted message.

To prevent the extra line breaks (which are converted to '<br />' for SMF 2.0.1x and '<br>' for SMF 2.1x) from being saved with the message make the following change to ./Sources/Subs-Post.php (comments optional):

For SMF 2.0.1x:
Code: [Select]
// Put it back together!
if (!$previewing)
$message = strtr(implode('', $parts), array('  ' => '&nbsp; ', "\n" => '<br />', $context['utf8'] ? "\xC2\xA0" : "\xA0" => '&nbsp;'));
else

Code: [Select]
// Put it back together!
if (!$previewing)
{
$message = strtr(implode('', $parts), array('  ' => '&nbsp; ', "\n" => '<br />', $context['utf8'] ? "\xC2\xA0" : "\xA0" => '&nbsp;'));

$breakFrom = "<br\s*?/?>";
$breakTo = "<br />";

// Replace two or more line breaks anywhere in the message with two line breaks.
$message = preg_replace('~(' . $breakFrom . $breakFrom . ')+~sim', $breakTo . $breakTo, $message);

// Remove leading/trailing line breaks from messages.
$message = preg_replace('~^(' . $breakFrom . ')+~sim', '', $message);
$message = preg_replace('~(' . $breakFrom . ')+$~sim', '', $message);

// Remove leading/trailing line breaks around quoted messages.
$message = preg_replace('~(' . $breakFrom . ')+\[quote~sim', $breakTo . '[quote', $message);
$message = preg_replace('~\[/quote\](' . $breakFrom . ')+~sim', '[/quote]' . $breakTo, $message);

// Remove leading/trailing line breaks within quoted messages.
$message = preg_replace('~\[quote(.*?)\](' . $breakFrom . ')+~sim', '[quote$1]' . $breakTo, $message);
$message = preg_replace('~(' . $breakFrom . ')+\[/quote\]~sim', $breakTo . '[/quote]', $message);
}
else


For SMF 2.1x:
Code: [Select]
// Restore white space entities
if (!$previewing)
$message = strtr($message, array('  ' => '&nbsp; ', "\n" => '<br>', $context['utf8'] ? "\xC2\xA0" : "\xA0" => '&nbsp;'));
else

Code: [Select]
// Restore white space entities
if (!$previewing)
{
$message = strtr($message, array('  ' => '&nbsp; ', "\n" => '<br>', $context['utf8'] ? "\xC2\xA0" : "\xA0" => '&nbsp;'));

$breakFrom = "<br\s*?/?>";
$breakTo = "<br>";

// Replace two or more line breaks anywhere in the message with two line breaks.
$message = preg_replace('~(' . $breakFrom . $breakFrom . ')+~sim', $breakTo . $breakTo, $message);

// Remove leading/trailing line breaks from messages.
$message = preg_replace('~^(' . $breakFrom . ')+~sim', '', $message);
$message = preg_replace('~(' . $breakFrom . ')+$~sim', '', $message);

// Remove leading/trailing line breaks around quoted messages.
$message = preg_replace('~(' . $breakFrom . ')+\[quote~sim', $breakTo . '[quote', $message);
$message = preg_replace('~\[/quote\](' . $breakFrom . ')+~sim', '[/quote]' . $breakTo, $message);

// Remove leading/trailing line breaks within quoted messages.
$message = preg_replace('~\[quote(.*?)\](' . $breakFrom . ')+~sim', '[quote$1]' . $breakTo, $message);
$message = preg_replace('~(' . $breakFrom . ')+\[/quote\]~sim', $breakTo . '[/quote]', $message);
}
else

Offline GigaWatt

  • The Smiley Guy
  • Support Specialist
  • SMF Hero
  • *
  • Posts: 1,441
  • Gender: Male
    • Macedonian electronics forum
Re: Removing extra line breaks from messages
« Reply #1 on: March 04, 2018, 08:47:19 AM »
Thanks ;). Just tried it, works like a charm (SMF 2.0.15) ;). I just left out this part.

Code: [Select]
// Replace two or more line breaks anywhere in the message with two line breaks.
$message = preg_replace('~(' . $breakFrom . $breakFrom . ')+~sim', $breakTo . $breakTo, $message);
"This is really a generic concept about human thinking - when faced with large tasks we're naturally inclined to try to break them down into a bunch of smaller tasks that together make up the whole."

"A 500 error loosely translates to the webserver saying, "WTF?"..."

Offline Rock Lee

  • Native Language Support Specialist
  • SMF Hero
  • *
  • Posts: 2,003
  • Gender: Male
  • I also speak english :D
    • BomberCode.Oficial on Facebook
    • RockLee-BC on GitHub
    • @Bomber_Code on Twitter
    • Bomber Code ~ La nueva era del conocimiento
Re: Removing extra line breaks from messages
« Reply #2 on: March 04, 2018, 08:57:12 AM »
It seems very good advice now when I'm at home I see if I connect and applied it :D!


Regards!
¡Regresando como cual Fenix! ~ Bomber Code © 2018
Ayudas - Aportes - Tutoriales - Y mucho mas!!!


Ayudame via PayPal

Offline skb

  • Jr. Member
  • **
  • Posts: 280
    • Metabolically Challenged
Re: Removing extra line breaks from messages
« Reply #3 on: March 10, 2018, 09:58:21 PM »
Would this work retrospectively or from the date of applying the change ?
Learning, every day.
Forum - The Diabetic Pub
Because every diabetic needs help
SMF 2.0.15 (default theme) / TP 1.42

Offline GL700Wing

  • Full Member
  • ***
  • Posts: 441
  • Gender: Female
Re: Removing extra line breaks from messages
« Reply #4 on: March 10, 2018, 10:15:55 PM »
Would this work retrospectively or from the date of applying the change ?
It won't update existing posts because they are already stored in the database.  However, any existing post that is modified or quoted after the tip is implemented will have extra line breaks removed.