Display Emoticons in Your Guestbook 
June 21st, 2008, 03:59 AM
| | Newbie | | Join Date: Jun 2008
Posts: 25
| |
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. -
<?php
-
function smiley2emoticons($text)
-
{
-
$tags = array
-
(
-
';)' => '__winking.gif__;)__',
-
':D' => '__biggrin.gif__:D__',
-
':-/' => '__confused.gif__:-/__',
-
'8->' => '__daydreaming.gif__8->__',
-
':P' => '__tongue.gif__:P__',
-
':-*' => '__kiss.gif__:-*__',
-
':-O' => '__surprise.gif__:-O__',
-
'X(' => '__angry.gif__X(__',
-
'B-)' => '__cool.gif__B-)__',
-
'>:)' => '__devil.gif__>:)__',
-
':((' => '__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 smiley2emoticons in action!
| 
June 24th, 2008, 11:50 PM
|  | Moderator | | Join Date: Jun 2007 Location: York, England, with wolves.
Posts: 4,858
| | | re: Display Emoticons in Your Guestbook
To get rid of the regex, you could do it like so: -
/* this is just an example */
-
$str = "happy sad";
-
$search = array("/happy/", "/sad/");
-
$replace = array(":)", ":(");
-
echo preg_replace($search, $replace, $str);
-
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|