Advertisement:

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

Offline GL700Wing

  • Jr. Member
  • **
  • Posts: 289
  • 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