Display Emoticons in Your Guestbook

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nashruddin
    New Member
    • Jun 2008
    • 25

    Display Emoticons in Your Guestbook

    Emoticons are very popular in the Internet these days. It seems like now every forums, mailing lists and guestbook support emoticons in their pages. If you write such applications, why not add them too? The basic concept is simple, all you need to do is replace smiley tags with the appropriate icons. For example, replace all :) with <img src="img/smile.gif" border="0" alt=":)" />

    Here's a simple example to convert smileys to emoticons. You can get the icons from Yahoo Messenger and rename the icons like smile.gif, sad.gif, angry.gif, etc.

    Code:
    <?php
    function smiley2emoticons($text)
    {
    	$tags = array
    	(
    		';)'  => '__winking.gif__;)__',
    		':D'  => '__biggrin.gif__:D__',
    		':-/' => '__confused.gif__:-/__',
    		'8->' => '__daydreaming.gif__8-&gt;__',
    		':P'  => '__tongue.gif__:P__',
    		':-*' => '__kiss.gif__:-*__',
    		':-O' => '__surprise.gif__:-O__',
    		'X('  => '__angry.gif__X(__',
    		'B-)' => '__cool.gif__B-)__',
    		'>:)' => '__devil.gif__&gt;:)__',
    		':((' => '__crying.gif__:((__',
    		':))' => '__laughing.gif__:))__',
    		':|'  => '__straightface.gif__:|__',
    		'=))' => '__rollingonthefloor.gif__=))__',
    		':-c' => '__callme.gif__:-c__',
    		':)]' => '__onthephone.gif__:)]__',
    		':-?' => '__thinking.gif__:-?__',
    		'#o'  => '__doh.gif__#o__',
    		':-w' => '__waiting.gif__:-w__',
    		':)'  => '__smile.gif__:)__',
    		':-)' => '__smile.gif__:)__',
    		':('  => '__sad.gif__:(__',
    		':-(' => '__sad.gif__:(__',
    		'(:|' => '__yawn.gif__(:|__'
    		
    	);
    	$new = strtr($text, $tags);
    	$new = preg_replace("/__([^_]*)__([^_]*)__/", "<img src=\"img/$1\" border=\"0\" alt=\"$2\" />", $new);
    	return($new);
    }
    ?>
    The function above will convert smiley tags to emoticons for the given text. Wanna see some demo? See smiley2emoticon s in action!
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    To get rid of the regex, you could do it like so:

    Code:
      /* this is just an example */
      $str = "happy sad";
      $search = array("/happy/", "/sad/");
      $replace = array(":)", ":(");
      echo preg_replace($search, $replace, $str);

    Comment

    Working...