Advertisement:

Author Topic: Removing extra leading/trailing line breaks in messages and quotes  (Read 280 times)

Offline GL700Wing

  • Jr. Member
  • **
  • Posts: 270
  • Gender: Female
I've noticed that 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 />";

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

// Remove leading/trailing line breaks around quoted messages.
$message = preg_replace('~(' . $breakFrom . ')+\[quote~sim', $breakTo . $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]',

// Replace three or more line breaks with two line breaks.
$message = preg_replace('~(' . $breakFrom . $breakFrom . $breakFrom . ')~sim', $breakTo . $breakTo, $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>";

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

// Remove leading/trailing line breaks around quoted messages.
$message = preg_replace('~(' . $breakFrom . ')+\[quote~sim', $breakTo . $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]',

// Replace three or more line breaks with two line breaks.
$message = preg_replace('~(' . $breakFrom . $breakFrom . $breakFrom . ')~sim', $breakTo . $breakTo, $message);
}
else
« Last Edit: December 20, 2017, 05:20:51 AM by GL700Wing »