Changing Text To Smilies.
Changing standard text into Smiley Images
Just for reference, I will be referring to the word "string" and "array" a lot in this tutorial. . There is more information on strings, arrays, and different types of variables Here and at PHP.net
This Tutorial uses the same idea as the BBCode and the Template tutorials. The basic idea behind turning plaintext smilies into images is this;
You take some text that you have stored in either files or mysql,
You tell php to find certain patterns in the text,
and then you tell php to replace those patterns with the html for an image.
There are many, many, many ways to do this, but I will be showing you a very simple way of doing it.
I will give you the code first then i will go throgh and explain it peice by peice.
PHP:<?php
$smiles = array(
':B'=>'<img src="buckteeth.gif" alt="" />',
'mad'=>'<img src="mad.gif" alt="" />');
$plain_text = 'I am more mad than an amish electrician :B';
$smiles_to_find = array_keys($smiles);
$images_to_replace = array_values($smiles);
$smile_text = str_replace($smiles_to_find,$images_to_replace,$plain_text);
echo $smile_text;
?>
Now lets break it down.
First I started by making an array of smiles. The smile that you want to find in the text is the key of one element of the array. You could make it anything that you wanted. The smile is followed by the img html that points to where the image is.
you can think of it like this you want to turn ;B into this <img src="buckteeth.gif" alt="" /> or
:B => <img src="buckteeth.gif" alt="" />
you can put as many smilies and images into this array as long as you don't put two of the same.
Next we defined our text. You could pull the text out of mysql, a file(file_get_contents()), or just use variables like i did, but it doesn't matter which method you use.
Next was this line.
PHP:<?php
$smiles_to_find = array_keys($smiles);
?>
This line basically just took all of the smiles to find and put them in their own array. In our case if i were to print_r() the $smiles_to_find array it would look like this:
Code:Array
(
[0] => :B
[1] => mad
)
Next I did a similar thing on the next line as well.
I took all of the images to find in the $smiles array and put them in their own array.
Again in our case if i were to print_r() the $images_to_replace array it would look like this:
Code:Array
(
[0] => <img src="buckteeth.gif" alt="" />
[1] => <img src="mad.gif" alt="" />
)
Now we have two arrays, the $smiles_to_find array and the $images_to_replace array. See how each smilie's key corrosponds to the images key. That will allow us to replace all of the $smiles_to_find with their corresponding images in $images_to_replace.
Now we will actually replace them. To do so we use the php function str_replace().
str_replace has 3 arguements. (str_replace( FIND , REPLACE , STRING ) )
It function works like this:
It looks in STRING for FIND and if it is there, it replaces it with REPLACE.
in our situation we will use arrays for FIND and REPLACE. so when it finds any one FIND it will replace it with the corresponding REPLACE.
when it is all done searching the text it returns the result to a new variable.
so our actual php will be like I posted earlier:
PHP:<?php
$smile_text = str_replace($smiles_to_find,$images_to_replace,$plain_text);
?>
all that we have left to do is echo the finished product
PHP:<?php
echo $smilie_text;
?>
Similar to my post on nl2br() This does alter the text so it is best to apply this to the text on display instead of before saving it.
ALSO Like i said ealier, you can add as many different smiles to this as you want, and you could tweak it to make it catch badwords as well. just put a badword for the smile to find(if you want to remove small words like ***, but still allow for assumption make the smile be ' *** ' w/o the quotes) and instead of placing an image there you can just put *** or other stuff.
___________________
Neverside Development Director
PHP Snippets
BigToach.com - IT WORKS, TOACHY!
Last edited by Noel, May 7th, 2005 10:37 PM (Edited 1 times)
