PHP Smilies System Tutorial

Ranked #2,306 in Internet, #135,010 overall

Creating a smilies system

In this tutorial we'll be creating a smilies system. What I mean by that is a function that converts smilies from text to images. This is not hard to accomplish and we'll be using a simple built-in PHP function str_replace to replace the smiley's text by its (html) image. First we'll have a look at the function str_replace:

str_replace($search, $replace, $string);

replaces $search by $replace in $string and returns the new string.

Knowing that, we could replace ( for example ) the smiley :) by the corresponding image for that smiley using the HTML image tag. For example:

$old_text = "some text with the smiley :)";
$new_text = str_replace(":)", "<img src='smiley.png' />");

Alright, so that's the principe, now it's time to create the actual function that does this for all smilies you wish.

The smilies function

Creating the function.

We'll now start creating the function that converts a plain text into a html text with smiley images in it ( replacing the smilies in the text by their images ). The function will need 2 arguments: the text (string) to replace smilies into and an array of all smilies and corresponding image files. Then all the function has to do is use str_replace to replace the smilies by their images. The array of all smilies to replace will be set up like this:

$smilies = array("smiley" => "image_file");

The keys of the array containing all smilies and the values of the array containing all filenames of the corresponding smiley images. So all our function needs to do is replace the array keys (=smiley text icons) by the html image created out of the array values (=smiley images). However there should be given a directory for all smilies ( which we'll set inside our config file (later) as $smilies_directory ). The path to the image for a smiley will be the smilies_directory / array values of $smilies (=filenames of images). But we don't want the smilies to be replaced by the paths of the images, but by the actual images. So we'll use the html img tag to do so, we do need to use the function array_map to convert each value of the array of images to the format <img src="smilies_directory/array value" /> where array value will be each value of the array ( all filenames of the smilies that need to be replaced ) and smilies_directory is the config variable $smilies_directory. To do this for each value of the array of smilies, we use the array_map function and create_function function to make a function that does this for all values of the array.

function replaceSmilies($str, $smilies) {

//make the config variable $smilies_directory global so that we can use it within this function even though it's defined outside of it
global $smilies_directory;

$icons = array_keys($smilies);
$images = array_map(create_function('$filename', 'global $smilies_directory; return "<img src=\"{$smilies_directory}/{$filename}\" />";'), array_values($smilies));

$new_str = str_replace($icons, $images, $str);

return($new_str);
}

We'll also create a standard array of smilies that will be automaticly used within the function when no other array ($smilies) is provided:

<?php

function replaceSmilies($str, $smilies = NULL) {

global $smilies_directory;

if(is_null($smilies)) {
global $default_smilies;
$smilies = $default_smilies;
}

$icons = array_keys($smilies);
$images = array_map(create_function('$filename', 'global $smilies_directory; return "<img src="\"{$smilies_directory}/{$filename}\"" />";'), array_values($smilies));

$new_str = str_replace($icons, $images, $str);

return($new_str);

}

?>

This way it will automaticly use the default smilies set/array ( which we'll create in a config file next) when no alternative smilies set is provided to the function.

Save the function inside of a file functions.php.

The config file

Creating the config file.

The config file is pretty simple: it basicly needs one array of smilies ( the default smilies set to use ) and the directory of the smilies images. For example:

config.php

<?php

$default_smilies = array(
":)" => "smile.gif",
":(" => "mad.gif",
":P" => "tongue.gif",
";)" => "wink.gif",
":D" => "biggrin.gif",
":/" => "frown.gif",
"o.O" => "eek.gif",
":?" => "confused.gif",
":cool:" => "cool.gif",
":rolleyes:" => "rolleyes.gif",
":redface:" => "redface.gif"
);

$smilies_directory = "images/smilies"; //the directory in which the images of the smilies are put

?>

Note: the smiley set used can be found here.

Usage

How to use our script.

To use our script we need to include the config file and the functions file into our PHP webpage and then we can simply call our function to replace smilies in any string.

<?php

include_once("config.php");
include_once("functions.php");

$string = "Hey there :D This is an example text :) Only a few smilies in this text but just an example ;).";

echo replaceSmilies($string);

?>

You might also like ...

Loading

Comments & Questions

Got any questions about the tutorial or just want to comment? This is the place to do so.

Related Products (Amazon)

Loading

Related Products (eBay)

Loading

by

webcodez

"Give a man a program and you'll frustrate him for one day. Teach a man to program and you'll frustrate him for a whole lifetime."

webcodez
GameHeroes
more »

Feeling creative? Create a Lens!