Log in

View Full Version : How to change this php function


Scandal
12-14-2010, 12:42 PM
Well, in functions.php, there is this function for the censorship system:
// ################################################## ###########################
/**
* Replaces any instances of words censored in $vbulletin->options['censorwords'] with $vbulletin->options['censorchar']
*
* @param string Text to be censored
*
* @return string
*/
function fetch_censored_text($text)
{
global $vbulletin;
static $censorwords;

if (!$text)
{
// return $text rather than nothing, since this could be '' or 0
return $text;
}

if ($vbulletin->options['enablecensor'] AND !empty($vbulletin->options['censorwords']))
{
if (empty($censorwords))
{
$vbulletin->options['censorwords'] = preg_quote($vbulletin->options['censorwords'], '#');
$censorwords = preg_split('#[ \r\n\t]+#', $vbulletin->options['censorwords'], -1, PREG_SPLIT_NO_EMPTY);
}

foreach ($censorwords AS $censorword)
{
if (substr($censorword, 0, 2) == '\\{')
{
if (substr($censorword, -2, 2) == '\\}')
{
// prevents errors from the replace if the { and } are mismatched
$censorword = substr($censorword, 2, -2);
}

// ASCII character search 0-47, 58-64, 91-96, 123-127
$nonword_chars = '\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f';

// words are delimited by ASCII characters outside of A-Z, a-z and 0-9
$text = preg_replace(
'#(?<=[' . $nonword_chars . ']|^)' . $censorword . '(?=[' . $nonword_chars . ']|$)#si',
str_repeat($vbulletin->options['censorchar'], vbstrlen($censorword)),
$text
);
}
else
{
$text = preg_replace("#$censorword#si", str_repeat($vbulletin->options['censorchar'], vbstrlen($censorword)), $text);
}
}
}

// strip any admin-specified blank ascii chars
$text = strip_blank_ascii($text, $vbulletin->options['censorchar']);

return $text;
}

When a message has censored words, this function replaces each character of the word with the censorchar.
I want to change this function to return the $text WITH the censored words, BUT beside each censored word, I want to there is a " " (space).
For example, if I write the censored word "example" I want to return " example".

I know it is droll, but I need it :D

Thanks in advance and sorry for my bad english.

Digital Jedi
12-14-2010, 07:51 PM
This might not be what you were going for, but why not just disable the censor and use Replacement Variables instead? Unless you were needing to recode for some reason.

Scandal
12-15-2010, 08:46 AM
This might not be what you were going for, but why not just disable the censor and use Replacement Variables instead? Unless you were needing to recode for some reason.
I need to use the censorship system.
I had achieved it before one year ago but I forget the changes. :(

It doen't need to recode all the function. Only the part of character replacement. I remember that it was a simple change to the function.

Well,
I have add the following code to functions_newpost.php
// Censorship Warning
$iwarncens = fetch_censored_text($post['message']);
if ($iwarncens != $post['message'])
{
$dataman->error('iwarn');
}
...and when the $post['message'] it is not the same with primary text, this code appears an error message.
The censorship system can change the $post['message']. If the cens system doesn't change the $post['message'] the error doesn't appear. ;)
So, I want to do an as far as possible simple change to the text.. not the replacement of all characters.

The code which I have add, works normally when the cens system change the text...

Thanks and sorry for my bad english..

kh99
12-15-2010, 12:02 PM
You maybe could change:

$text = preg_replace("#$censorword#si", str_repeat($vbulletin->options['censorchar'], vbstrlen($censorword)), $text);


to

$text = preg_replace("#$censorword#si", " $censorword", $text);


There is also another preg_replace() line above that where you could make a similar change although I'm not sure if that would do what you want.

Scandal
12-15-2010, 12:30 PM
Thanks kh99, it works fine! :D