News:

Wondering if this will always be free?  See why free is better.

Main Menu

Trim Subject and Message Body

Started by MobileCS, November 15, 2017, 10:15:01 PM

Previous topic - Next topic

MobileCS

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

Suki

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>
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

MobileCS

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

Suki

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.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

MobileCS

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.

Suki

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
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

MobileCS

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;

Suki

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.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

MobileCS

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

Suki

Indeed. You are completely right, an oversight on my part. Trick has been approved.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

Advertisement: