Simple Machines Community Forum

Customizing SMF => Tips and Tricks => Thema gestartet von: MobileCS in November 15, 2017, 10:15:01 NACHMITTAGS

Titel: Trim Subject and Message Body
Beitrag von: MobileCS in November 15, 2017, 10:15:01 NACHMITTAGS
(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:

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

// 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.
Titel: Re: Trim Subject and Message Body
Beitrag von: Suki in November 17, 2017, 10:39:28 VORMITTAG
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>
Titel: Re: Trim Subject and Message Body
Beitrag von: MobileCS in November 17, 2017, 11:23:04 VORMITTAG
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   />.
Titel: Re: Trim Subject and Message Body
Beitrag von: Suki in November 21, 2017, 11:48:00 VORMITTAG
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:


<br> <br > <br   > <br/> <br /> <br   />some text<br> <br />


It will only remove the first br tag.
Titel: Re: Trim Subject and Message Body
Beitrag von: MobileCS in November 21, 2017, 03:40:04 NACHMITTAGS
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.
Titel: Re: Trim Subject and Message Body
Beitrag von: Suki in November 30, 2017, 11:46:19 VORMITTAG
Tried the new regex with the same example:


<br> <br > <br   > <br/> <br /> <br   />some text<br> <br />


it doesn't recognize the brs after the text
Titel: Re: Trim Subject and Message Body
Beitrag von: MobileCS in November 30, 2017, 04:24:50 NACHMITTAGS
How are you testing it? It works just fine here :

$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;
Titel: Re: Trim Subject and Message Body
Beitrag von: Suki in Dezember 08, 2017, 05:44:26 NACHMITTAGS
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.
Titel: Re: Trim Subject and Message Body
Beitrag von: MobileCS in Dezember 09, 2017, 04:54:52 NACHMITTAGS
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
Titel: Re: Trim Subject and Message Body
Beitrag von: Suki in Dezember 09, 2017, 06:03:36 NACHMITTAGS
Indeed. You are completely right, an oversight on my part. Trick has been approved.