Replace Character Using PHP for Creating Own Smiley Code

0
66

Good day everyone! This time, I am just going to teach you some basic tutorial for replacing characters. But before that, I have a simple question to you. Do you ever try on posting a statement or message with emoticons/smileys? Usually, we do it when we are chatting with our friends on FACEBOOK. You just type the characters, for example, “ : ) ” without spaces then after that it will show the emoticon. Do you ever wonder how does it work? Well, this simple tutorial gives you some basic knowledge in regards to creating your own emoticon code and replace it to an emoticon.

Before that, I will give you first some knowledge about a PHP function that we will use in creating our smiley code.

str_replace is PHP function which uses for replacing a character, word or statement into a new value.

For example:

We will replace small letter “b” into capital letter “B” or we will replace small letter “a” into an integer “1”.

*** REPLACING SMALL LETTER “b” into capital letter “B” ***

  1. <?php
  2.  
  3. $letter = “b”;
  4.  
  5. $new_letter_format = “B”;
  6.  
  7. $statement = “boy”;
  8.  
  9. $convert = str_replace($letter, $new_letter_format, $statement);
  10.  
  11. echo $convert;
  12.  
  13. ?>

Output:

Did you get the idea in replacing characters in PHP?

Now we will use that idea for developing our own smiley code.

So first, download emoticons, or create your own emoticon if you are good in graphic designing. Here, I just downloaded 5 emoticons which are angel, angry, cool, cry and smile.

So what we do now is to display any of 5 smileys you want to show without typing “<img src=”” />”. What we need is just to type its corresponding codes like “ : ) ” without spaces or maybe we “[smile]” or anything you want.

To do this, just write the following codes.

  1. &lt;?php
  2.  
  3. $emoticon_codes = array([angel],[angry],[cool]” “[cry],[smile]);
  4.  
  5. $emoticons  = array(&lt;img src=”angel.gif” /&gt;,&lt;img src=”angry.gif” /&gt;,&lt;img src=”cool.gif” /&gt;,&lt;img src=”cry.gif” /&gt;,&lt;img src=[smile]/&gt;);
  6.  
  7. $statement = "Hi! [angel]";
  8.  
  9. $convert_emoticon_code = str_replace($emoticon_codes, $emoticons, $statement);
  10.  
  11. echo $convert_emoticon_code;
  12.  
  13. ?&gt;

Output:

If you have questions about Replace Character Using PHP for Creating Personalized Smiley Codes 2016, feel free to comment here or visit our contact page. Thank you.

Facebook Comments
(Visited 64 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here