News:

SMF 2.1.6 has been released! Take it for a spin! Read more.

Main Menu

Adding filter when posting

Started by simzman, August 28, 2012, 05:46:05 AM

Previous topic - Next topic

simzman

Hi,

I'd like to add this
<?php
$text
= preg_replace('~\s{2,}~', ' ', $text);
?>

To run when you have written your post and are clicking 'send' to remove any double spacing.
However, I can't figure out where to insert the code so that it runs as you click post.

Thank you in advance.

Orangine


simzman

Thanks Orangine,

I will add a censor - and that will fix some instances. But the main reason for doing this is once the posts has been submitted, doubles spaces become: [space]+[&nbsp]
I have censors for &nbsp however in most cases they are up against other words and cannot be pick up and removed. This is the major problem. Many posts with &nbsp throughout other words. A lot of maintenance. Was aiming to intercept the message prior to it being 'sent' or posted.

Many thanks for your quick response and help!
Perhaps you or others have had a similar problem? Haven't found a solution of SMF forums as yet.


Orangine

ok, got it now

look into ./Sources/Subs-Post.php and search for $mistake_fixes = array(

simzman

Excellent, will try it and keep you posted.
:D

simzman

Ok, so found  ./Sources/Subs-Post.php and $mistake_fixes = array(
So I added the following and it returned a blank post error, wiping the whole post.

$mistake_fixes = array(
// Find Double space and replace with single space
$text = str_replace('  ', ' ', $text),


Also tried some variations, like
$mistake_fixes = array(
// Find Double space and replace with single space
$text = str_replace('  ', ' ', $text),

Still the same error message when posting.

Here is the full section of that original code
$mistake_fixes = array(
// Find [table]s not followed by [tr].
'~\[table\](?![\s' . $non_breaking_space . ']*\[tr\])~s' . ($context['utf8'] ? 'u' : '') => '[table][tr]',
// Find [tr]s not followed by [td].
'~\[tr\](?![\s' . $non_breaking_space . ']*\[td\])~s' . ($context['utf8'] ? 'u' : '') => '[tr][td]',
// Find [/td]s not followed by something valid.
'~\[/td\](?![\s' . $non_breaking_space . ']*(?:\[td\]|\[/tr\]|\[/table\]))~s' . ($context['utf8'] ? 'u' : '') => '[/td][/tr]',
// Find [/tr]s not followed by something valid.
'~\[/tr\](?![\s' . $non_breaking_space . ']*(?:\[tr\]|\[/table\]))~s' . ($context['utf8'] ? 'u' : '') => '[/tr][/table]',
// Find [/td]s incorrectly followed by [/table].
'~\[/td\][\s' . $non_breaking_space . ']*\[/table\]~s' . ($context['utf8'] ? 'u' : '') => '[/td][/tr][/table]',
// Find [table]s, [tr]s, and [/td]s (possibly correctly) followed by [td].
'~\[(table|tr|/td)\]([\s' . $non_breaking_space . ']*)\[td\]~s' . ($context['utf8'] ? 'u' : '') => '[$1]$2[_td_]',
// Now, any [td]s left should have a [tr] before them.
'~\[td\]~s' => '[tr][td]',
// Look for [tr]s which are correctly placed.
'~\[(table|/tr)\]([\s' . $non_breaking_space . ']*)\[tr\]~s' . ($context['utf8'] ? 'u' : '') => '[$1]$2[_tr_]',
// Any remaining [tr]s should have a [table] before them.
'~\[tr\]~s' => '[table][tr]',
// Look for [/td]s followed by [/tr].
'~\[/td\]([\s' . $non_breaking_space . ']*)\[/tr\]~s' . ($context['utf8'] ? 'u' : '') => '[/td]$1[_/tr_]',
// Any remaining [/tr]s should have a [/td].
'~\[/tr\]~s' => '[/td][/tr]',
// Look for properly opened [li]s which aren't closed.
'~\[li\]([^\[\]]+?)\[li\]~s' => '[li]$1[_/li_][_li_]',
'~\[li\]([^\[\]]+?)\[/list\]~s' => '[_li_]$1[_/li_][/list]',
'~\[li\]([^\[\]]+?)$~s' => '[li]$1[/li]',
// Lists - find correctly closed items/lists.
'~\[/li\]([\s' . $non_breaking_space . ']*)\[/list\]~s' . ($context['utf8'] ? 'u' : '') => '[_/li_]$1[/list]',
// Find list items closed and then opened.
'~\[/li\]([\s' . $non_breaking_space . ']*)\[li\]~s' . ($context['utf8'] ? 'u' : '') => '[_/li_]$1[_li_]',
// Now, find any [list]s or [/li]s followed by [li].
'~\[(list(?: [^\]]*?)?|/li)\]([\s' . $non_breaking_space . ']*)\[li\]~s' . ($context['utf8'] ? 'u' : '') => '[$1]$2[_li_]',
// Allow for sub lists.
'~\[/li\]([\s' . $non_breaking_space . ']*)\[list\]~' . ($context['utf8'] ? 'u' : '') => '[_/li_]$1[list]',
'~\[/list\]([\s' . $non_breaking_space . ']*)\[li\]~' . ($context['utf8'] ? 'u' : '') => '[/list]$1[_li_]',
// Any remaining [li]s weren't inside a [list].
'~\[li\]~' => '[list][li]',
// Any remaining [/li]s weren't before a [/list].
'~\[/li\]~' => '[/li][/list]',
// Put the correct ones back how we found them.
'~\[_(li|/li|td|tr|/tr)_\]~' => '[$1]',
// Images with no real url.
'~\[img\]https?://.{0,7}\[/img\]~' => '',
);


Lastly tried this:
while (strpos($text,'  ') !== false)
{
        $str = str_replace('  ',' ',$str);
}


Posting worked again, but no change on the double spacing...
I'm still very new to PHP, and I don't have a strong understanding of the syntax yet. I found these double space fixes on the net. I didn't write them... Hope you can help.

Thank you.

MrPhil

Your change didn't work because $mistake_fixes is an array of strings (regular expression patterns to search for, and their replacement strings). You tried adding an executable statement in the middle of the array!

Find this:
$mistake_fixes = array(
// Find [table]s not followed by [tr].
'~\[table\](?![\s' . $non_breaking_space . ']*\[tr\])~s' . ($context['utf8'] ? 'u' : '') => '[table][tr]',

and insert the new term:
$mistake_fixes = array(
            // replace runs of multiple whitespaces by single blanks
           '~\s{2,}~'  => ' ',
// Find [table]s not followed by [tr].
'~\[table\](?![\s' . $non_breaking_space . ']*\[tr\])~s' . ($context['utf8'] ? 'u' : '') => '[table][tr]',


Note that \s can include line-ends (returns), so you may want to explicitly list what characters are to be replaced, e.g.,
$mistake_fixes = array(
            // replace runs of multiple whitespaces by single blanks
           '~[ \t' . $non_breaking_space . ']{2,}~'  => ' ',
// Find [table]s not followed by [tr].
'~\[table\](?![\s' . $non_breaking_space . ']*\[tr\])~s' . ($context['utf8'] ? 'u' : '') => '[table][tr]',

to replace blanks, tabs, and non-breaking spaces (but not line-ends) with single blanks. It may take some experimentation -- I have not tested this, and don't know if $mistake_fixes is even the right place for this.

simzman

Thank you Mr Phil. Very informative post, I am yet to even learn the principals of structuring PHP  :-[

Quote from: MrPhil on August 29, 2012, 10:56:59 AM
$mistake_fixes = array(
            // replace runs of multiple whitespaces by single blanks
           '~[ \t' . $non_breaking_space . ']{2,}~'  => ' ',
// Find [table]s not followed by [tr].
'~\[table\](?![\s' . $non_breaking_space . ']*\[tr\])~s' . ($context['utf8'] ? 'u' : '') => '[table][tr]',


The above code did the trick! Adding the non_breaking_space meant that returning a new line was not affected. It intercepted double and triple spacing and replaced it with one.

I'm in the process of testing it now, but after a few minutes of trialling it appears to be exactly what I was after. Are there any tweaks you suggest looking into?

Advertisement: