Hello!
In this tutorial we'll be goingto create a smilies system.
So you can use smilies in your text by just using: : ) or : D, etc!
We'll be doing it the same as with the BBcodes system but then for our smilies!
Let's start!
The smilies
First we'll make an function for it.
We'll call it: smilies.
Since a function works like this:
our function will look like this:
Now $text should contain the string where the smilies codes should be replaced to images by.
An example to run the function and give a value to $text:
Oke, lets start with the content of the function.
We'll start with creating an variable for the smilie codes and images.
Once we made an array of those; : ) will be replaced by smilie.gif and : D will be replaced by: bigrin.gif, etc..
Now we have to set it into an array, we'll use the function: explode() for this.
This function works as follow:
it will explode the variable in an array, each string separated by [char].
So for example:
will produce the array:
but this:
will also produce the array:
well, in our case is each smilie code and images separated by: ,
so we'll use:
Oke, let's do it.
So $smilies will make an array from the string $smilie_codes, each array part seperated by : , in the $smilie_codes.
And for $images the same but then for the string $smilie_images.
$smilies[0] will be replaced by $images[0](this will replace : ) by smilie.gif), $smilies[1] by $images[1], etc..
Let's put it together and set it into our function.
Now we'll count the amount of smilies (array parts). We'll use the function count() for this.
This functions works as follow:
So in our case the arrays are in the variable: $smilies and $images.
We'll just count one of them, cause they should those have the same amount of array parts if correct.
Now $amount will contain the amount of string parts(smilie codes) there are in the array $smilies.
Let's put all together to see what we have now.
Now we'll make a loop,and since $smilies[0](: )) should be replaced by $images[0](smilie.gif)[/b] we can make an integer variable which will hold the array number, cause the ones which are equal to eachother should be replaced($smilies[1] by $images[1], etc.), and then let it replace them by eachother if there's one found in the string we want to replace it in, and then increase it untill it's equal to the last array number(which is equal to $amount).
We'll use the for() loop; if you've no idea what this is, I advice you to have a look at this tutorial.
We set $i equal to 0 (the first array part/string number) and will increase it each time, untill it's equal to the last one(which number is in $amount generated).
Now we have to replace everything.
We'll be using the function str_replace for this, it's good enough to let this function do it.
So how the function works?
So in our case we want to replace all smilie codes ($smilies[$i]) by the smilie images($images[$i]) in the string $text.
now $text will contain the new text, the same variable so, it will change all smilie codes by the correct smilie images which should be at the same array ("part/string") number.
In this example you see it will be replaced by:
so in this case the smilies should be located in the folder 'images', if not, our change the directory correctly or change the code
It should be in the for loop, so lets do it.
Now we only have to let our function return the new text which is in the variable $text cause there we replaced all smilies in correctly by the smilie images.
We'll use the Return() function for this.
In our case is the variable $text so it will look like:
Oke, so now: let's put EVERYTHING together for OUR FINAL RESULT ! ! !
And you're done!
You can now add images and smilie codes by yourself, upload some images in the correct name as you did in the script to to a folder; call the folder 'images' or change the script if you called it else or just have it in the root directory.
You can now try it out:
Enjoy!
Good luck, and I hope you'd understand my tutorial
If you have any questions, feel free to ask me!
Skyfe.
In this tutorial we'll be goingto create a smilies system.
So you can use smilies in your text by just using: : ) or : D, etc!
We'll be doing it the same as with the BBcodes system but then for our smilies!
Let's start!
The smilies
First we'll make an function for it.
We'll call it: smilies.
Since a function works like this:
CODE
function function_name($variables)
{
//write something here
}
{
//write something here
}
our function will look like this:
CODE
function smilies($text)
{
...
}
{
...
}
Now $text should contain the string where the smilies codes should be replaced to images by.
An example to run the function and give a value to $text:
CODE
echo smilies("a test :D");
//or even:
smilies("another test;)");
//or even:
smilies("another test;)");
Oke, lets start with the content of the function.
We'll start with creating an variable for the smilie codes and images.
CODE
//our smilie codes
$smilie_codes = ":), :D, :P, :S, :(";
//our smilie images
$smilie_images = "smilie.gif, bigrin.gif, thoung.gif, weird.gif, mad.gif";
$smilie_codes = ":), :D, :P, :S, :(";
//our smilie images
$smilie_images = "smilie.gif, bigrin.gif, thoung.gif, weird.gif, mad.gif";
Once we made an array of those; : ) will be replaced by smilie.gif and : D will be replaced by: bigrin.gif, etc..
Now we have to set it into an array, we'll use the function: explode() for this.
This function works as follow:
CODE
explode("[char]", $[variable]);
it will explode the variable in an array, each string separated by [char].
So for example:
CODE
$testvar = "part1, part2, part3";
$array_testvar = explode(" ,", $testvar);
$array_testvar = explode(" ,", $testvar);
will produce the array:
CODE
$array_testvar = array("part1", "part2", "part3");
but this:
CODE
$testvar = "part1; part2; part3;";
$array_testvar = explode(";", $testvar);
$array_testvar = explode(";", $testvar);
will also produce the array:
CODE
$array_testvar = array("part1", "part2", "part3");
well, in our case is each smilie code and images separated by: ,
so we'll use:
CODE
explode(", ", $var);
Oke, let's do it.
CODE
//set our smilie codes into an array
$smilies = explode(", ", $smilie_codes);
//set our smilie images into an array
$images = explode(", ", $smilie_images);
$smilies = explode(", ", $smilie_codes);
//set our smilie images into an array
$images = explode(", ", $smilie_images);
So $smilies will make an array from the string $smilie_codes, each array part seperated by : , in the $smilie_codes.
And for $images the same but then for the string $smilie_images.
$smilies[0] will be replaced by $images[0](this will replace : ) by smilie.gif), $smilies[1] by $images[1], etc..
Let's put it together and set it into our function.
CODE
function smilies($text)
{
//our smilie codes
$smilie_codes = ":), :D, :P, :S, :(";
//our smilie images
$smilie_images = "smilie.gif, bigrin.gif, thoung.gif, weird.gif, mad.gif";
//set our smilie codes into an array
$smilies = explode(", ", $smilie_codes);
//set our smilie images into an array
$images = explode(", ", $smilie_images);
}
{
//our smilie codes
$smilie_codes = ":), :D, :P, :S, :(";
//our smilie images
$smilie_images = "smilie.gif, bigrin.gif, thoung.gif, weird.gif, mad.gif";
//set our smilie codes into an array
$smilies = explode(", ", $smilie_codes);
//set our smilie images into an array
$images = explode(", ", $smilie_images);
}
Now we'll count the amount of smilies (array parts). We'll use the function count() for this.
This functions works as follow:
CODE
$variable = count($[array]);
So in our case the arrays are in the variable: $smilies and $images.
We'll just count one of them, cause they should those have the same amount of array parts if correct.
CODE
$amount = count($smilies);
Now $amount will contain the amount of string parts(smilie codes) there are in the array $smilies.
Let's put all together to see what we have now.
CODE
function smilies($text)
{
//our smilie codes
$smilie_codes = ":), :D, :P, :S, :(";
//our smilie images
$smilie_images = "smilie.gif, bigrin.gif, thoung.gif, weird.gif, mad.gif";
//set our smilie codes into an array
$smilies = explode(", ", $smilie_codes);
//set our smilie images into an array
$images = explode(", ", $smilie_images);
$amount = count($smilies)-1; //we'll decrease it by 1, since all array's start with the number [0] and not [1], and counting of them DOES start by1, so if we want to know the last array number string, we gotta decrease it by 1
}
{
//our smilie codes
$smilie_codes = ":), :D, :P, :S, :(";
//our smilie images
$smilie_images = "smilie.gif, bigrin.gif, thoung.gif, weird.gif, mad.gif";
//set our smilie codes into an array
$smilies = explode(", ", $smilie_codes);
//set our smilie images into an array
$images = explode(", ", $smilie_images);
$amount = count($smilies)-1; //we'll decrease it by 1, since all array's start with the number [0] and not [1], and counting of them DOES start by1, so if we want to know the last array number string, we gotta decrease it by 1
}
Now we'll make a loop,and since $smilies[0](: )) should be replaced by $images[0](smilie.gif)[/b] we can make an integer variable which will hold the array number, cause the ones which are equal to eachother should be replaced($smilies[1] by $images[1], etc.), and then let it replace them by eachother if there's one found in the string we want to replace it in, and then increase it untill it's equal to the last array number(which is equal to $amount).
We'll use the for() loop; if you've no idea what this is, I advice you to have a look at this tutorial.
CODE
for($i=0;$i<=$amount;$i++)
{
//replace $smilies[$i] by $images[$i] since $i will be equal to the array number and the strings from the same array number of the array from $smilies should be replaced by the string of the SAME array number from the array of $images
}
{
//replace $smilies[$i] by $images[$i] since $i will be equal to the array number and the strings from the same array number of the array from $smilies should be replaced by the string of the SAME array number from the array of $images
}
We set $i equal to 0 (the first array part/string number) and will increase it each time, untill it's equal to the last one(which number is in $amount generated).
Now we have to replace everything.
We'll be using the function str_replace for this, it's good enough to let this function do it.
So how the function works?
CODE
$var = str_replace([string that should be replaced], [replace it by this], [string where it should happene]);
So in our case we want to replace all smilie codes ($smilies[$i]) by the smilie images($images[$i]) in the string $text.
CODE
$text = str_replace($smilies[$i], "<img src=images/".$images[$i]." border=0>", $text);
now $text will contain the new text, the same variable so, it will change all smilie codes by the correct smilie images which should be at the same array ("part/string") number.
In this example you see it will be replaced by:
CODE
<img src=images/the_smilie_image.gif border=0>
so in this case the smilies should be located in the folder 'images', if not, our change the directory correctly or change the code
It should be in the for loop, so lets do it.
CODE
for($i=0;$i<=$amount;$i++)
{
$text = str_replace($smilies[$i], "<img src=images/".$images[$i]." border=0>", $text);
}
{
$text = str_replace($smilies[$i], "<img src=images/".$images[$i]." border=0>", $text);
}
Now we only have to let our function return the new text which is in the variable $text cause there we replaced all smilies in correctly by the smilie images.
We'll use the Return() function for this.
CODE
Return $var;
In our case is the variable $text so it will look like:
CODE
Return $text;
Oke, so now: let's put EVERYTHING together for OUR FINAL RESULT ! ! !
CODE
function smilies($text)
{
//our smilie codes
$smilie_codes = ":), :D, :P, :S, :(";
//our smilie images
$smilie_images = "smilie.gif, bigrin.gif, thoung.gif, weird.gif, mad.gif";
//set our smilie codes into an array
$smilies = explode(", ", $smilie_codes);
//set our smilie images into an array
$images = explode(", ", $smilie_images);
$amount = count($smilies)-1; //we'll decrease it by 1, since all array's start with the number [0] and not [1], and counting of them DOES start by1, so if we want to know the last array number string, we gotta decrease it by 1
for($i=0;$i<=$amount;$i++)
{
$text = str_replace($smilies[$i], "<img src=images/".$images[$i]." border=0>", $text);
}
Return $text;
}
{
//our smilie codes
$smilie_codes = ":), :D, :P, :S, :(";
//our smilie images
$smilie_images = "smilie.gif, bigrin.gif, thoung.gif, weird.gif, mad.gif";
//set our smilie codes into an array
$smilies = explode(", ", $smilie_codes);
//set our smilie images into an array
$images = explode(", ", $smilie_images);
$amount = count($smilies)-1; //we'll decrease it by 1, since all array's start with the number [0] and not [1], and counting of them DOES start by1, so if we want to know the last array number string, we gotta decrease it by 1
for($i=0;$i<=$amount;$i++)
{
$text = str_replace($smilies[$i], "<img src=images/".$images[$i]." border=0>", $text);
}
Return $text;
}
And you're done!
You can now add images and smilie codes by yourself, upload some images in the correct name as you did in the script to to a folder; call the folder 'images' or change the script if you called it else or just have it in the root directory.
You can now try it out:
CODE
$test_message = "Cool! :D Look, it works :P I can use smilies now! :)";
$replaced_test_message = smilies($test_message); //will contain the new message with the smilie images
//echo it
echo $replaced_test_message;
$replaced_test_message = smilies($test_message); //will contain the new message with the smilie images
//echo it
echo $replaced_test_message;
Enjoy!
Good luck, and I hope you'd understand my tutorial
If you have any questions, feel free to ask me!
Skyfe.
discuss this topic to forum
