Customizing SMF > Tips and Tricks

Removing extra line breaks from messages

(1/1)

GL700Wing:
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: --- // Put it back together!
if (!$previewing)
$message = strtr(implode('', $parts), array('  ' => '&nbsp; ', "\n" => '<br />', $context['utf8'] ? "\xC2\xA0" : "\xA0" => '&nbsp;'));
else
--- End code ---


--- Code: --- // 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
--- End code ---


For SMF 2.1x:

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


--- Code: --- // 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
--- End code ---

Navigation

[0] Message Index

Go to full version