admin管理员组

文章数量:1360367

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.

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.

Share Improve this question edited Oct 29, 2012 at 3:12 Chase Florell 47.5k59 gold badges190 silver badges382 bronze badges asked Aug 18, 2012 at 15:45 Stanislas PiotrowskiStanislas Piotrowski 2,70211 gold badges41 silver badges60 bronze badges 4
  • 2 Wele 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 Commented Aug 18, 2012 at 15:50
  • really want to do that in js on the client ..meaning someone disabling it will see all original values? – definitely undefinable Commented Aug 18, 2012 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 Commented Aug 18, 2012 at 15:50
  • in fact it is for the pany 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 Commented Aug 18, 2012 at 16:00
Add a ment  | 

7 Answers 7

Reset to default 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 remend going with a service that already has this figured out:

  • http://www.webpurify./
  • Look at this SO post: How do you implement a good profanity filter?

I'm going to use CoffeeScript, you can pile 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)

If the page content is as well ing 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,'**');

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.

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
});

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.

Full disclosure, I wrote the plugin.

I've written a jQuery plugin that does what you're looking for. It is not pletely 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./

And here's some example code.

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

<script>
    $('#someDiv').profanityFilter({
        customSwears: ['ass']
    });
</script>

本文标签: javascriptsystem for censoring banned wordsStack Overflow