MM 4023 -- Creation a Language Filter Function in PHP
mini project 15 (developed by JD)

Instruction:

Using the provided PHP script, build your own.
a) Go through the following steps to understand the code.
b) Change the code a little bit.
c) Set up your localhost and your_username folder
d) Copy and save your code in your C drive
f) Create your localhost and your_username folder in the C drive by using the animated procedure


Step 1 : Set the PHP Tags


The first step in creating any PHP script is to type the php opening and closing tags. The body of your PHP code will always fall within these tags.


Step 2 : Define the Function

 


When creating a function choose a name that is relevant to the task the function will perform. In this case, we will name the function "language_filter" because this function will filter profane words out of a string sent to it. The $string variable will contain the message you pass to the language_filter function. Remember, standard function creation consists of:


    function my_function_name(){
        //function contents
    }

You may also pass an optional parameter by declaring it in the functions title like so:

    function my_function_name($passed_parameter){
        print $passed_parameter;
    }


Step 3 : Create an Array() of bad words


Now that we have created the language_filter function's frame, lets add the contents. We will start by creating an array() of all the bad words we want to filter out. We use an array because it is essentially a list of data type values. These data types can be numbers, strings, objects, and even other arrays. In this example our curse words are "curse", "word", " foul ", and "language". The curse words are stored in the array $obscenities.


Step 4 : The 'for loop'


Now that we have created a list of curse words we should iterate through the list, checking the string for each curse word. In order to iterate through the list of curse words we will use a "foreach" loop. For those of you familiar with JavaScript a "foreach" loop is equivalent to a "for in" loop. For every word in the list it will perform the code between its brackets. With each pass of the for loop the variable $curse_word is set a value in the array $obscenities.


Step 5 : Does $string contain $obscenities ?


Now that the variable $curse_word is set to a word in $obscenities we will check the $string to see if it contains the $curse_word. We will use two PHP functions, stristr() and trim(), to accomplish this. The stristr() method helps you find a nettle in a hay stack. The needle, in this case, is the value in $curse_word and the hay stack is the entire message stored in $string. The trim() method will remove leading and trailing whitespace, a.k.a. spaces, from the string. The "if statement" will evaluate to the boolean value of true if the $curse_word is found and false if it is not. If the curse word is not found the "for each" will move to the next value stored in $obscenities.


Step 6 : Masking the bad ****s


We have found a curse word, now what? We will mask the curse word with *'s! To do this we will use a "for loop". The "for loop" will loop for each letter of the curse word. We find the number of letters, or length, of the curse word by using strlen(). The length of the $curse_word is stored in the variable $length. Every pass of the "for loop" will append a "*" to the variable $star. The "." is a concat operator, so $stars .= "*" tells the value of "*" to be appended to the end of the string $stars. You may also type $stars = $stars+"*";. After the $stars string is filled with *'s we will use the eregi_replace() method to replace the $curse_word in the $string with $stars and set the variable $string to its new filtered form. We then reset the $stars string to "" and get ready for the next loop of the "foreach" statement.


Step 7 : Return the filtered string


After all the words in $obscenities have been looped through we will return the new and curse word free value of $string.


Step 8 : Calling the language_filter Function


This is an example of how to use the language_filter function. The print command is used to display the returned filtered string from the language_filter(). There are still things you could add to this function. Some possible additions are a more advanced word substitution, retrieving the curse word list from an external file, and email notification when profane messages are posted.


Step 9 : Copy The Code


Now that we have reviewed the code it is time to give it a test drive. I have provided the code for you to copy and paste into a new text document. I choose to supply the code after the instruction, because I believe comprehension of the code should be achieved so that this is not a trivial copy and past exercise.


Step 10 : Saving the PHP file


Once you have copied and paste the code into a new text document, save it as 'filter.php' in the 'C:\apachefriends\xampp\htdocs\<your_username>' directory. You may test this script by going to http://localhost/<your_username>/filter.php. For information about setting up your localhost and your_username folder please click here.


Dr. Johari
Mini Project 15