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 ---

GigaWatt:
Thanks ;). Just tried it, works like a charm (SMF 2.0.15) ;). I just left out this part.


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

Rock Lee:
It seems very good advice now when I'm at home I see if I connect and applied it :D!


Regards!

skb:
Would this work retrospectively or from the date of applying the change ?

GL700Wing:

--- Quote from: skb on March 10, 2018, 09:58:21 PM ---Would this work retrospectively or from the date of applying the change ?

--- End quote ---
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.

Navigation

[0] Message Index

Go to full version