...

View Full Version : comments posted - wish to replace obscene words with hashed and stared (substitute)



jasonc310771
12-30-2007, 11:03 AM
I have been trying to figure this out but not quiet sure how to do this effectively.

say some one posts a comment to a forum and they have words that are not suitable for family viewing, i wish to have these words substituted with other words like for example, s*it instead of the obvious word that is was.

i think i need the words to be in an array, maybe from a database where the list can be added to when new words are used that need changing.

any ideas on how i actually change the words in a long variable from one word to another.


thanks in advance for your help.


and happy new year.

oesxyl
12-30-2007, 11:30 AM
$obscene = array('word1', 'word2', ....);
$regobscen = array();
foreach($obscen as $word){
$regword = '/' . $word . '/';
$regobscen[$tegword] = preg_replace('/./','*',$word);
}
$what = array_keys($regobscene);
$with = array_values($regobscene);
$comment = preg_replace($what,$with,$comment);


I don't test it and I don't check the sintax.

This is men against the machine, I bet you can't win. If you discover that somebody use 'aba' and you add to
obscene words, you'll find later spelled as 'a b a', 'a$b a' or 'A b A', or other variants.

best regards and happy new year

Inigoesdr
12-30-2007, 12:22 PM
You could shorten that a bit like this:

$replace = array('word1', 'word2');
foreach($replace as $k => $word)
{
unset($replace[$k]);
$replace[$word] = str_repeat('*', strlen($word));
}

$comment = str_replace(array_keys($replace), array_values($replace), $comment);

oesxyl
12-30-2007, 12:40 PM
You could shorten that a bit like this:

$replace = array('word1', 'word2');
foreach($replace as $k => $word)
{
unset($replace[$k]);
$replace[$word] = str_repeat('*', strlen($word));
}

$comment = str_replace(array_keys($replace), array_values($replace), $comment);

agree with that, but must be corrected because the items of array wich is first argument of the preg_replace must be beetwen '/'.
solution is simple:



$replace = array('/word1/', '/word2/', ...);


and



$replace[$word] = str_repeat('*', strlen($word)-2);


PS: I think that preg_replace_callback could be a beter way.

best regards and happy new yesr

Inigoesdr
12-31-2007, 02:03 AM
agree with that, but must be corrected because the items of array wich is first argument of the preg_replace must be beetwen '/'.
solution is simple:



$replace = array('/word1/', '/word2/', ...);


and



$replace[$word] = str_repeat('*', strlen($word)-2);


I didn't use preg_replace().

PS: I think that preg_replace_callback could be a beter way.
You could use preg_replace_callback(), but it's not necessary here.

oesxyl
12-31-2007, 02:21 AM
I didn't use preg_replace().

sorry and thank you for replay, I was blind, :)

my previos replay is incorrect and useles, :thumbsup:



You could use preg_replace_callback(), but it's not necessary here.

I'm thinking to a shorter and maybe complex solution, I mean to replace
misspeled words which avoid ban and other thing.
Anyway this could become more complex then moderating comments, :)

best regards and happy new year



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum