Advertisement:

Author Topic: Trim Subject and Message Body  (Read 326 times)

Offline MobileCS

  • Jr. Member
  • **
  • Posts: 124
Trim Subject and Message Body
« 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.
« Last Edit: December 09, 2017, 04:51:27 PM by MobileCS »

Offline Suki

  • Customizer
  • SMF Super Hero
  • *
  • Posts: 15,086
  • Kaizoku Jotei
    • MissAllSunday on GitHub
    • SMF mods
Re: Trim Subject and Message Body
« Reply #1 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>
Look at them. They're just asking for it. Maybe the human race deserves to be wiped out.

Offline MobileCS

  • Jr. Member
  • **
  • Posts: 124
Re: Trim Subject and Message Body
« Reply #2 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   />.

Offline Suki

  • Customizer
  • SMF Super Hero
  • *
  • Posts: 15,086
  • Kaizoku Jotei
    • MissAllSunday on GitHub
    • SMF mods
Re: Trim Subject and Message Body
« Reply #3 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.
Look at them. They're just asking for it. Maybe the human race deserves to be wiped out.

Offline MobileCS

  • Jr. Member
  • **
  • Posts: 124
Re: Trim Subject and Message Body
« Reply #4 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.

Offline Suki

  • Customizer
  • SMF Super Hero
  • *
  • Posts: 15,086
  • Kaizoku Jotei
    • MissAllSunday on GitHub
    • SMF mods
Re: Trim Subject and Message Body
« Reply #5 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
Look at them. They're just asking for it. Maybe the human race deserves to be wiped out.

Offline MobileCS

  • Jr. Member
  • **
  • Posts: 124
Re: Trim Subject and Message Body
« Reply #6 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;

Offline Suki

  • Customizer
  • SMF Super Hero
  • *
  • Posts: 15,086
  • Kaizoku Jotei
    • MissAllSunday on GitHub
    • SMF mods
Re: Trim Subject and Message Body
« Reply #7 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.
Look at them. They're just asking for it. Maybe the human race deserves to be wiped out.

Offline MobileCS

  • Jr. Member
  • **
  • Posts: 124
Re: Trim Subject and Message Body
« Reply #8 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

Offline Suki

  • Customizer
  • SMF Super Hero
  • *
  • Posts: 15,086
  • Kaizoku Jotei
    • MissAllSunday on GitHub
    • SMF mods
Re: Trim Subject and Message Body
« Reply #9 on: December 09, 2017, 06:03:36 PM »
Indeed. You are completely right, an oversight on my part. Trick has been approved.
Look at them. They're just asking for it. Maybe the human race deserves to be wiped out.