1

I'm actually working on a website in which I'll need to replace many words by something like for example: banana by ******.

I use a website with php and mysql, but I also use javascript.

I have in my database a table in which are banned words.

I'm receive this words in an array from my database. i'm looking for a function that will be able to replace this words in all tha page. i can not use function like ob start.

The best will be a function that check on body onload and replace words.

Chase Florell
  • 46,378
  • 57
  • 186
  • 376
Stanislas Piotrowski
  • 2,595
  • 10
  • 40
  • 59
  • 2
    Welcome to Stack Overflow Stanislas! Do you have code in place to get your banned words to your Javascript? If not, then you'll need to think about how that will work. Is every user going to have to download every swear word on every page load? A server side solution is much preferred here. – Rich Bradshaw Aug 18 '12 at 15:50
  • really want to do that in js on the client ..meaning someone disabling it will see all original values? – definitely undefinable Aug 18 '12 at 15:50
  • Why do you want to do it in the page, instead I'd server-side or when it's added to the database? – Eric Aug 18 '12 at 15:50
  • in fact it is for the company I work in. they ask me to do something like that because we may have control from the CNIL, in english it is board which enforces law on data protection. I just need to disaple it for the intranet, not employee wil desactive javascript. – Stanislas Piotrowski Aug 18 '12 at 16:00

7 Answers7

4

This is a rather difficult task to tackle because:

  1. People will try to circumvent this system by replacing certain letter, such as "s" with "$", "a" with "@", or by misspelling words that can still be understood
  2. How will you deal with words like "password" that contains an swear word?

I would recommend going with a service that already has this figured out:

Community
  • 1
  • 1
Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
  • n fact it is for the company I work in. they ask me to do something like that because we may have control from the CNIL, in english it is board which enforces law on data protection. I just need to disaple it for the intranet, not employee wil desactive javascript. – Stanislas Piotrowski Aug 18 '12 at 16:01
  • Well no system is perfect. I tried webpurify, and it said there was no profanity, when I wrote 'fok you human' – Max Jul 05 '15 at 21:26
2

I'm going to use CoffeeScript, you can compile to JavaScript here if you wish or just use this as pseudocode.

String::replaceAll = (a, b) ->
  regExp = new RegExp(a, "ig")
  @replace regExp, b

_stars = (string) ->
  str = ""
  for i in [0..string.length]
    str = "#{str}*"

  str

bannedWords = [ "bannedword", "anotherbannedword" ]

_formSubmitHandler = (data) ->
  for bannedWord in bannedWords
    data.userInput = data.userInput.replaceAll bannedWord, _stars(data.userInput)
sciritai
  • 3,688
  • 1
  • 17
  • 22
  • You should use it when the form which contains the user data is submitted. – sciritai Aug 18 '12 at 16:08
  • I understand but it is not often possible because I have more that 12000 form in the intranet service. So it means that I will have to change them all. I need to find an other issue. but thanks for the help. – Stanislas Piotrowski Aug 18 '12 at 16:16
  • I see, is it true that all of these forms make different requests? There is no way to centralise the filtering of data? – sciritai Aug 18 '12 at 16:21
  • yeah because it is for a cabinet that makes debt recovery. so they have many forms for different actions, like add payment, edit payment, add client, edit client, add debtor, edit debtor, and for all action in relationship with their profession. – Stanislas Piotrowski Aug 18 '12 at 16:24
1

If the page content is as well coming from the database, or being entered into the database. Why not filter it using php prior to it being inserted or when it is pulled using str_replace

// PREFERRED WAY
$filteredContent = str_replace($bannedlist, "**", $content2Filter);

Or if you are looking for a javascript version, then you would need to use either multiple str.replace or regex. Something like:

var search = "/word1|word2|word3/gi"; //This would be your array joined by a pipe delimiter
var ret=str.replace(search,'**');
James Williams
  • 4,221
  • 1
  • 19
  • 35
1

I made a very simple censoring method for this. It will only track words you put into the array of bad words. I would suggest you use an advanced library for word censors.

censor.js

var censor = (function() {
    function convertToAsterisk(word) {
        var asteriskSentence = '';
        for(var asterisks=0;asterisks<word.length;asterisks++) {
            asteriskSentence+='*';
        }
        return asteriskSentence;
    }

    return function(sentence, bannedWords) {
        sentence    = sentence      || undefined;
        bannedWords = bannedWords   || undefined;

        if(sentence!==undefined && bannedWords!==undefined) {
            for(var word=0;word<bannedWords.length;word++) {
                sentence = sentence.replace(bannedWords[word], convertToAsterisk(bannedWords[word]));
            }
        }

        return sentence;
    };
})();

The method can be used like so:

var sentence = 'I like apples, grapes, and peaches. My buddy likes pears';
var bannedWords = [
    'pears',
    'peaches',
    'grapes',
    'apples'
];
sentence = censor(sentence, bannedWords);

This system does not protect bad words within other words, or tricky mispellings. Only the basics.

Max
  • 2,710
  • 1
  • 23
  • 34
0
var str="badword";
var ret=str.replace("badword","*******");

And to detect length automatically (useful for function useage)

var str="badword";
var ret=str.replace("badword",function() {
    var ret = ""
    for(var loop = 0; loop < str.length; loop++) {
        var ret = ret + "*"
    }
    return ret
});
0

Finally I find my own way to make this system it is an easy way and you don't need to change all the code for all your website just the page that needs to be censored.

As far as I'm concerned I uses thausands of pages but the things is that I have one main page that included others pages.

For poeple who may be interested. all you have to do is to put this code at the beginning of your page so after the just put this code <?php ob_start(); ?> at the end of the body, before just put this code `

      <?php   
        //We get the content of the page
        $content = ob_get_contents(); 
        // and we replace all 
        $content = str_replace('naughty', '*****', $content); 

        /// / VERY important, we must finish the page or in any case include ob_end_clean () function before echo $ content as PHP code would be displayed also
    ob_end_clean ();
echo $content; 
?>

This is an easy way, but you can also do an array for all censored words.

Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
Stanislas Piotrowski
  • 2,595
  • 10
  • 40
  • 59
0

Full disclosure, I wrote the plugin.

I've written a jQuery plugin that does what you're looking for. It is not completely water tight, and others can very easily circumvent the plugin by disabling javascript. If you'd like to try it out, here's a link.

http://profanityfilter.chaseflorell.com/

And here's some example code.

<div id="someDiv">swears are ass, but passwords are ok</div>

<script>
    $('#someDiv').profanityFilter({
        customSwears: ['ass']
    });
</script>
Chase Florell
  • 46,378
  • 57
  • 186
  • 376