Question:

How to make a blacklist of swear words in my game.?

by  |  earlier

0 LIKES UnLike

I would like to make a script in php that stops anyone writing swear words or foreign URLS (mainly other game sites) on my game, is there a script already made to do this because i dont know where to start making this script.

Any help would be much appreciated

 Tags:

   Report

2 ANSWERS


  1. Here's what I would do...

    The function will return strings that LOOK bad along with their positions... then what you can do with the returned array is either write a function to assign a point value of HOW bad it seems like if there's no http:// and the entire domain is less than 7-10 characters, you might assign it a low value and let it pass, if it has http:// you're probably going to give it a very high value and deny the input.  Alternatively you might send the input back to the user with the questionable strings highlighted and ask them to update their input so that it will not contain unauthorized strings.

    Hope this helps...



    $INVALID_STRINGS = array( 'bad language', 'frackenfrit' );

    $VALID_URL_DOMAINS = array( 'mydomain.com', 'mydomain.net' );

    function tokenScreen($input) {



    global $INVALID_STRINGS, $VALID_URL_DOMAINS;



    foreach($INVALID_STRINGS as $is) {

    if($pos = strpos($input, $is)) {

    $tokens[] = array( 'type'=>'invalid_string', 'position'=>$pos, 'string'=>$is );

    }

    }

    preg_match_all("/([A-Za-z]+:\\/\\/)?(... ?dot ?))+[A-Za-z]{2,6})(\\/[^ ]*| )?/ims",$input, $matches);

    foreach($matches[2] as $index=>$match) {

    if( !in_array($match,$VALID_URL_DOMAINS) ) {

    $tokens[] = array( 'type'=>'possible_url', 'full_url_position'=>strpos($input, $matches[0][$index]), 'full_url'=>$matches[0][$index],'domain_... 'domain'=>$match );

    }

    }



    return $matches;

    }

    $input = "This is some input with bad language like frackenfrit and outside urls: www.elephant.com/apple.html babies.com jackiechan.com/page1.php http://whatyaknow.com, but http://mydomain.com/file.html or mydomain.net/file.php is ok.but if someone screws up and forgets to put a space after a (period) example.there could be problems. good thing is the regex will match somedomain dot com, bad thing is if someone were just typing I love a dot now and then, we catch that too.";

    echo '<pre>';

    print_r(tokenScreen($input));

    echo '</pre>';

    ===OUTPUT===

    Array

    (

        [0] => Array

            (

                [type] => invalid_string

                [position] => 24

                [string] => bad language

            )

        [1] => Array

            (

                [type] => invalid_string

                [position] => 42

                [string] => frackenfrit

            )

        [2] => Array

            (

                [type] => possible_url

                [full_url_position] => 72

                [full_url] => www.elephant.com/apple.html

                [domain_position] => 72

                [domain] => www.elephant.com

            )

        [3] => Array

            (

                [type] => possible_url

                [full_url_position] => 100

                [full_url] => babies.com

                [domain_position] => 100

                [domain] => babies.com

            )

        [4] => Array

            (

                [type] => possible_url

                [full_url_position] => 111

                [full_url] => jackiechan.com/page1.php

                [domain_position] => 111

                [domain] => jackiechan.com

            )

        [5] => Array

            (

                [type] => possible_url

                [full_url_position] => 136

                [full_url] => http://whatyaknow.com

                [domain_position] => 143

                [domain] => whatyaknow.com

            )

        [6] => Array

            (

                [type] => possible_url

                [full_url_position] => 221

                [full_url] => ok.but

                [domain_position] => 221

                [domain] => ok.but

            )

        [7] => Array

            (

                [type] => possible_url

                [full_url_position] => 293

                [full_url] => example.there

                [domain_position] => 293

                [domain] => example.there

            )

        [8] => Array

            (

                [type] => possible_url

                [full_url_position] => 361

                [full_url] => somedomain dot com

                [domain_position] => 361

                [domain] => somedomain dot com

            )

        [9] => Array

            (

                [type] => possible_url

                [full_url_position] => 429

                [full_url] => a dot now

                [domain_position] => 429

                [domain] => a dot now

            )

    )


  2. Whenever someone is able to send text that you wan't to filter out the blacklisted items, you need to put it into a method which takes in a string and returns the filtered strign

    Example:

    function filter($cleanString){

    //Filter here

    return $filteredString;

    }

    Where it says "//Filter here" you need to remove the words you don't want

    http://uk3.php.net/manual/en/function.st...  this url will tell you how you can replace a word with what you want

    I would suggest putting this filter method inside a separate php file such as filter.php which you then include wherever it is needed.

    Now for the words, you can either create an array inside the filter.php class of all the words to be removed, you could also make an array of replacements for the words, or just statically set the same replacement for every word. OR you could store this information in a table inside a mysql database.

    For urls its a bit harder as you need to look for url patterns

    a simple way it could be done is by looking for http:// and www.  and removing everything until the next space and looking for .com .co.uk .org and .net (or others) and removing everything Before upto the next space

    This example might be useful for finding these occurences

    http://www.phpro.org/examples/Find-Posit...

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.