How to add smilies in comments text and blog posts with simple PHP function?


Smileys are great way of expressing emotions with images/icons. You cant think of chatting with your friends without smileys. Am I right?

smileys 27 yearsIt’s been over 27 years when text smiley introduced also it’s been so long when smileys came out from IMs (Yahoo Messenger, Windows Messenger, etc) to comments on websites, blog posts. People use them to express the right amount of emotions and feelings they feel. You can see hundreds different smileys which helps people to show exact match of their expressions.

If you are using any of IMs available today, then I am sure you are aware of how you write a smiley in chat window. you just type the code like : ) and it converts into smiley like this :) Similarly, you use different smiley codes to show different smiley emotion icon. This article will help you to use the similar feature in your own web projects, commenting systems, blog posts, etc.

So ready to add smileys support into your web application, blog or any other PHP project? Lets follow the article below.

Step 1: Intro, get ready
First of all you need the images(icons) of smileys you would like to use. I prefer to use 16×16 sized images. There are many icon libraries freely available on internet. I am sharing few of them with you:

Step 2: The function
Create a function in PHP, which will parse the smileys code into its equivalent smiley image.

/*-----------------------------------------------------------------------------
    @Function    :  addSmilies()
    @Params      :  1. $text: Text to render.
    @Description :  This functions replaces the basic smiley equavalent codes
                    into smilies and returns back the html code after adding smilies
                    images
    @Returns     :  Returns the converted html string.

    NOTE         :  Make sure you provide the image path correctly. Use absolute or relative
                    paths, whatever suits to your script.
    -----------------------------------------------------------------------------*/
    function addSmilies($text) {
        //codes and its relevant smiley image path
        $codesToConvert = array(
                ':)'    =>  'smile',
                ':-)'   =>  'smile',
                ':D'    =>  'laugh',
                ':d'    =>  'laugh',
                ';)'    =>  'wink',
                ':P'    =>  'tounge',
                ':-P'   =>  'tounge',
                ':-p'   =>  'tounge',
                ':p'    =>  'tounge',
                ':('    =>  'sad face',
                ':o'    =>  'shock',
                ':O'    =>  'shock',
                ':0'    =>  'shock',
                ':|'    =>  'straight face',
                ':-|'   =>  'straight face'
               );
        //Replaces smilies codes / add smilies images: IMG html tags and return rendered text
        return (strtr($text, $codesToConvert));

    }//END addSmilies()

Step 3: Usage
Lets see how to use the above function in PHP code to actually parse the smileys code.

$comment = 'This is the example comment with few smilies code we use
normally. Like these: :)  :D  :P  :p :O :-|  . Its funny and cute. Isnt it? :) ';

    //prints text as it is, adding just to make it visually good looking
    echo 'Text with smiley codes:' . $comment ;

    //parse text and convert smilies codes into images, lets see
    echo 'Text with smiley images:' . addSmilies($comment);

I have created a full example script and live demo for you. See links below:

About SachinKRaj

Sachin is a web application developer, technology blogger and web addict! He has over 6 years of web development experience and he writes tutorials primarily focused on LAMP, Ajax, Api's, jQuery etc. He is usability expert and he always likes to share his knowledge with people.

, , , ,

19 Responses to How to add smilies in comments text and blog posts with simple PHP function?

  1. Alok July 22, 2009 at 10:05 am #

    very intersting article,

  2. Gaurav July 22, 2009 at 10:28 am #

    Awesome, this is very easy to use in our php script .Thanks Sach !!

  3. shubhamoy July 22, 2009 at 12:43 pm #

    Much awaited article from Sachin. Really thanks a ton for your support :)

  4. anand November 2, 2009 at 6:39 pm #

    Really thanks to u for this kind of intersting article

  5. test December 22, 2009 at 12:58 pm #

    no comment :cool:

  6. oempak June 8, 2010 at 6:11 pm #

    cool, I must try this, thanks :)

  7. The Doctor July 12, 2010 at 10:28 am #

    Hey huge thanks for this. Was looking for this exact solution for a few days now that was well commented.

    Thankyou for sharing :)

  8. Migu August 23, 2010 at 6:15 am #

    :)

  9. Su... September 3, 2010 at 6:18 pm #

    Its really cool dude.. thanks…

  10. girish October 27, 2010 at 12:41 pm #

    how to use this smiles in text box

  11. dan December 19, 2010 at 7:03 am #

    thank you sachin for this php function, it's realy easy to implement and add new emoticons.

  12. deepikavasudeva December 29, 2010 at 9:23 pm #

    not able to… can u plz tell how to use the php file?

  13. deepika December 29, 2010 at 9:55 pm #

    not able to do it…HELP!

  14. why doesnt wokr February 4, 2011 at 11:17 pm #

    <img src="http://paants.com/imhotter/inc/emotes2/smile.gif&quot; width="18px">

  15. reenez September 3, 2011 at 11:40 pm #

    this is really good :|

  16. Udara January 1, 2012 at 9:51 pm #

    Thank you very much for your guidance. I reffered number of sites. But as a beginner I couldn’t find any simple and understandable guidance like this.
    Thanks again

  17. Natthamonkan Kp. January 5, 2012 at 8:55 am #

    That’s great! :)

Leave a Reply