Log in

View Full Version : [Tut] Creating a World Filter and Smily Parser



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.

Jae
05-08-2006, 06:53 AM
Very good tut might use the word censor for next security upgrade at europehabbbo :P

Lycan
05-08-2006, 10:00 AM
remind me to do that next tme i'm bored and decide i need a decent website

Flauvo
05-08-2006, 10:05 AM
Good tut but web design how?

The Voice
05-08-2006, 10:35 AM
Nice tut. I'll use that in the making of my CMS

Oracle:
05-08-2006, 01:14 PM
Good tut but web design how?

Web Design isn't just the design aspect or coding aspect, the category should be named Web Development. Using PHP to do things like this or with XML etc come under that, this would be useful for a custom PHP/ASP forum if you were to re-code it in ASP, but also for a CMS..

So stop being arrogant and learn how to be constructive.

Mentor
05-08-2006, 03:51 PM
Good tut but web design how?
Well most obviously in terms of it being used for Webdesign. if your designing a CMS for a website, filtering bad words or parsing smilys are often an important part. Hence are part of designing that website.

Next you'l be asking how a webpage layout is webdesign...

Bandersnatch
06-08-2006, 05:32 AM
Great work and tutorial, well done :D

frozen.
07-08-2006, 06:06 AM
**Removed**

Edited by Bomb-Head (Forum Moderator): Please don't avoid the filter, thanks :)

bob
07-08-2006, 09:43 AM
**Text removed from quote**

You see these hatin' wiggas, I got mode between the furniz, they bump and they gone. How we go low, dont do that. GANGSTA, don't Callie know? Tell her now, before she pops up. We go spit it limb from limb, you know the crones got it, we ridin' till 25. You talkin bull, you motha ya bruv and ya dog, gangsta gangsta im a fking rider.

** U MAN.

Fujitsu
09-08-2006, 10:24 AM
Wow frozen, your soo cool. Not. Great two tuts ;)

Want to hide these adverts? Register an account for free!