Simple Machines Community Forum

Customizing SMF => Tips and Tricks => Topic started by: MobileCS on November 15, 2017, 10:15:01 PM

Title: Trim Subject and Message Body
Post by: MobileCS on November 15, 2017, 10:15:01 PM
(SMF 2.0.xx Only)

This code is for users who want to trim any whitespace from either side of the " subject title " and remove any blank lines from the start -and- end of the message body.

Sources\Post.php

Find:
Code: [Select]
// Collect all parameters for the creation or modification of a post.
$msgOptions = array(
'id' => empty($_REQUEST['msg']) ? 0 : (int) $_REQUEST['msg'],
'subject' => $_POST['subject'],
'body' => $_POST['message'],

Replace:

Code: [Select]
// Collect all parameters for the creation or modification of a post.
$msgOptions = array(
'id' => empty($_REQUEST['msg']) ? 0 : (int) $_REQUEST['msg'],
'subject' => trim($_POST['subject']),
'body' => preg_replace('/^(\s*<br\s*\/?>\s*)+|(\s*<br\s*\/?>\s*)+$/', '', $_POST['message']),

This code will trigger when creating new messages and modifying them.
Title: Re: Trim Subject and Message Body
Post by: Suki on November 17, 2017, 10:39:28 AM
Regex should be used as a last resort thing, for this specific case, trim() will do the job just fine unless you somehow allow HTML in the subject too.

If you still want to go with regex, the one you're using will fail with an html5 br tag: <br>
Title: Re: Trim Subject and Message Body
Post by: MobileCS on November 17, 2017, 11:23:04 AM
SMF converts all carriage returns to "<br />" for message text. That is why I am using regex. In which cases would this not happen?

I've modified my code so the regex handles <br>, <br >, <br   >, <br/>, <br />, <br   />.
Title: Re: Trim Subject and Message Body
Post by: Suki on November 21, 2017, 11:48:00 AM
Indeed it does. The reason why I suggested trim() is because catching all variations with regex its complicated, for example, your updated regex will still fail with a text like this one:

Code: [Select]
<br> <br > <br   > <br/> <br /> <br   />some text<br> <br />

It will only remove the first br tag.
Title: Re: Trim Subject and Message Body
Post by: MobileCS on November 21, 2017, 03:40:04 PM
How can trim() be used for $_POST['message']? When I tried, it did nothing.

After checking why things weren't working, I saw that "$_POST['message']" contained actual "<br />" characters - so regex is our only option for the message text.

I've updated my regex so it fixes your issue, and also trims any possible whitespace before or after the <br> tags.
Title: Re: Trim Subject and Message Body
Post by: Suki on November 30, 2017, 11:46:19 AM
Tried the new regex with the same example:

Code: [Select]
<br> <br > <br   > <br/> <br /> <br   />some text<br> <br />

it doesn't recognize the brs after the text
Title: Re: Trim Subject and Message Body
Post by: MobileCS on November 30, 2017, 04:24:50 PM
How are you testing it? It works just fine here :

Code: [Select]
$text = "<br> <br > <br   > <br/> <br /> <br   />some text<br> <br />";
$trim = preg_replace('/^(\s*<br\s*\/?>\s*)+|(\s*<br\s*\/?>\s*)+$/', '', $text);
echo $trim;
Title: Re: Trim Subject and Message Body
Post by: Suki on December 08, 2017, 05:44:26 PM
Sorry for the late reply, I'm testing it on: https://regex101.com/r/oG9hwU/1  since I currently do not have a test environment.   Will try to check it over the weekend.
Title: Re: Trim Subject and Message Body
Post by: MobileCS on December 09, 2017, 04:54:52 PM
The reason it wasn't working in regex101 is that the "g" flag is not enabled by default. When using preg_replace, it is enabled by default.

https://regex101.com/r/oG9hwU/7
Title: Re: Trim Subject and Message Body
Post by: Suki on December 09, 2017, 06:03:36 PM
Indeed. You are completely right, an oversight on my part. Trick has been approved.