PHP Smilies System Tutorial
Ranked #3,073 in Internet, #174,756 overall
Creating a smilies system
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' />");
The smilies function
Creating the function.
$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);
}
<?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);
}
?>
Save the function inside of a file functions.php.
The config file
Creating the config file.
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
?>
Usage
How to use our script.
<?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 ...
Related Products (Amazon)
Related Products (eBay)
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 »
- 21 featured lenses
- Winner of 14 trophies!
- Top lens » PHP & MySQL Poll System Tutorial
Explore related pages
- PHP & MySQL Poll System Tutorial PHP & MySQL Poll System Tutorial
- PHP & MySQL Login System Tutorial PHP & MySQL Login System Tutorial
- PHP & MySQL Visitors Counter Tutorial PHP & MySQL Visitors Counter Tutorial
- PHP basics tutorial PHP basics tutorial
- PHP Banner Rotator Tutorial PHP Banner Rotator Tutorial
- PHP Sessions and Cookies Tutorial PHP Sessions and Cookies Tutorial