Mentor
04-08-2006, 11:57 PM
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.
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;
}
Now to filter those words you just run apply the function to the input
$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 ****"
Word Filtering Breakdown
Ok now lets look at how the word filtering works.
function filter($msg)
{
We set up the function
$bad_words = explode(',', "tomato,lettuce,carrot,potato,broccoli,cucumber,pea" );
Now we create an array of all are bad words. The array in the example is "tomato,lettuce,carrot,potato,broccoli,cucumber,pea" Change this to your needs, simply seperate every word with a comma.
foreach ($bad_words as $naughty)
{
$msg = eregi_replace($naughty, "****", $msg);
}
Now we loop threw the array and run an eregi_replace on each, substituting **** for each of the words in the bad words list.
return $msg;
}
Then We return the filtered variable and end the function.
Making Smiles
In this part im gonna quickly go threw how to convert text smiley to there image equivalents.
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;
}
This can be called in pretty much the same way as the first
$input = "Hey there :D how are you all ;)";
$output = doSmily($input);
echo $output;
//This would then print out the message complete with image smilys
Making Smiles Breakdown
Now Lets look at how it works.
function doSmily($msg)
{
We open the Function
$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);
We use the string replace method to look threw the string and replace any occurrence of the smiley with the relevant image code.
$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.
return $msg;
}
Then finally we return the value
iCrag (Forum Moderator): Good tutorial, moved to Website Tutorials.
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.
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;
}
Now to filter those words you just run apply the function to the input
$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 ****"
Word Filtering Breakdown
Ok now lets look at how the word filtering works.
function filter($msg)
{
We set up the function
$bad_words = explode(',', "tomato,lettuce,carrot,potato,broccoli,cucumber,pea" );
Now we create an array of all are bad words. The array in the example is "tomato,lettuce,carrot,potato,broccoli,cucumber,pea" Change this to your needs, simply seperate every word with a comma.
foreach ($bad_words as $naughty)
{
$msg = eregi_replace($naughty, "****", $msg);
}
Now we loop threw the array and run an eregi_replace on each, substituting **** for each of the words in the bad words list.
return $msg;
}
Then We return the filtered variable and end the function.
Making Smiles
In this part im gonna quickly go threw how to convert text smiley to there image equivalents.
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;
}
This can be called in pretty much the same way as the first
$input = "Hey there :D how are you all ;)";
$output = doSmily($input);
echo $output;
//This would then print out the message complete with image smilys
Making Smiles Breakdown
Now Lets look at how it works.
function doSmily($msg)
{
We open the Function
$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);
We use the string replace method to look threw the string and replace any occurrence of the smiley with the relevant image code.
$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.
return $msg;
}
Then finally we return the value
iCrag (Forum Moderator): Good tutorial, moved to Website Tutorials.