-

Shout Box

Loading...
Shoutbox requres javascript enabled to work.
-

Affiliates.

-

Gallery

Image Gallery offline
-

Forum Activity

Last post by Brother Ryan

Last post by Brother Ryan

yoyo
( 0 replies )
Last post by James

Greetings
( 0 replies )
Last post by James

Last post by James

Welcome to Thybag6 (Alpha 4)
This site is still in alpha, meaning that it is not yet feature complete. Most sections of the site are now online, although there are still a number of key content areas and advanced functionalty to be added.
Please report any errors you find to admin@thybag.co.uk
-

php Tutorial

Rate this Tutorial?
885
Tutorial views
Creating a World Filter and Smiley Parser
This tutorial aims to show you how to create a basic Word filter and smiley Parser, which you can then use on anything from guest books to comments systems on your own sites.

Submitted by Bag
-

Creating a World Filter and Smiley Parser

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.

PHP
<?php

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
PHP
<?php


$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.

PHP
<?php

function filter($msg)
{

?>

We set up the function
PHP
<?php

$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.
PHP
<?php

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.
PHP
<?php

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.

PHP
<?php

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
PHP
<?php


$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.

PHP
<?php

function doSmily($msg)
{

?>

We open the Function
PHP
<?php

$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.
PHP
<?php

return $msg;
}

?>

Then finally we return the value