In this tutorial im going to show you how to create a basic Word filter and smiley Parser. Its Split in to 4 parts. The first 2 parts are about the word filter, firstly its code and secondly a breakdown of the code.
The 3rd and 4th part follow the same patten except on the topic Smilys parsing.
I hope this is helpful to some people.
Word Filtering
First off we want to create are filter function. In the case of the example i will be using healthy foods instead of swear words.
Now to filter those words you just run apply the function to the inputPHP Code:
function filter($msg)
{
$bad_words = explode(',', "tomato,lettuce,carrot,potato,broccoli,cucumber,pea" );
foreach ($bad_words as $naughty)
{
$msg = eregi_replace($naughty, "****", $msg);
}
return $msg;
}
Word Filtering BreakdownPHP Code:
$input = "Hello i am a carrot salesman made of lettuce";
$output = filter($input);
echo $output;
//This would then print out, "Hello i am a **** salesman made of ****"
Ok now lets look at how the word filtering works.
We set up the functionPHP Code:
function filter($msg)
{
Now we create an array of all are bad words. The array in the example is "tomato,lettuce,carrot,potato,broccoli,cucumber,pe a" Change this to your needs, simply seperate every word with a comma.PHP Code:
$bad_words = explode(',', "tomato,lettuce,carrot,potato,broccoli,cucumber,pea" );
Now we loop threw the array and run an eregi_replace on each, substituting **** for each of the words in the bad words list.PHP Code:
foreach ($bad_words as $naughty)
{
$msg = eregi_replace($naughty, "****", $msg);
}
Then We return the filtered variable and end the function.PHP Code:
return $msg;
}
Making Smiles
In this part im gonna quickly go threw how to convert text smiley to there image equivalents.
This can be called in pretty much the same way as the firstPHP Code:
function doSmily($msg)
{
$msg = str_replace(':)', '<img src="Smileys/smile.gif" alt=":)" />', $msg);
$msg = str_replace(':(', '<img src="Smileys/sad.gif" alt=":(" />', $msg);
$msg = str_replace(':D', '<img src="Smileys/biggrin.gif" alt=":D" />', $msg);
$msg = str_replace(';)', '<img src="Smileys/wink.gif" alt=";)" />', $msg);
$msg = str_replace(':o', '<img src="Smileys/ohmy.gif" alt=":o" />', $msg);
return $msg;
}
Making Smiles BreakdownPHP Code:
$input = "Hey there :D how are you all ;)";
$output = doSmily($input);
echo $output;
//This would then print out the message complete with image smilys
Now Lets look at how it works.
We open the FunctionPHP Code:
function doSmily($msg)
{
We use the string replace method to look threw the string and replace any occurrence of the smiley with the relevant image code.PHP Code:
$msg = str_replace(':)', '<img src="Smileys/smile.gif" alt=":)" />', $msg);
$msg = str_replace(':(', '<img src="Smileys/sad.gif" alt=":(" />', $msg);
$msg = str_replace(':D', '<img src="Smileys/biggrin.gif" alt=":D" />', $msg);
$msg = str_replace(';)', '<img src="Smileys/wink.gif" alt=";)" />', $msg);
$msg = str_replace(':o', '<img src="Smileys/ohmy.gif" alt=":o" />', $msg);
$msg = str_replace(':)', '<img src="Smileys/smile.gif" alt=":)" />', $msg);
The first bold part is the smiley. And the second is the image code which is what we replace it with.
You can easily change this to your requirements.
Then finally we return the valuePHP Code:
return $msg;
}
iCrag (Forum Moderator): Good tutorial, moved to Website Tutorials.