Replace emoticons into real icons

Emoticons are known as ASCII glyphs originally designed to show an emotional state in plain text messages. [1] With Tavi's parse-engine we can easily replace pre-defined emoticons by graphic icons.

Here is the 'Tavi site with graphic emoticons done by us: [noellab.net]

The emoticons to be replaced as the following patch are: :-) :-( :P XD =.= ^.^. You can modify the settings for other replacement as you wish.

How to Install

Affected files

Graphic Icons

Put the graphic icons ([donwload]) into your image directory, for example, ./img.

Here is Microsoft's emoticons: http://messenger.msn.it/Resource/Emoticons.aspx

./config.php

Add the following statements:

// Directory of Icons
$ImgDir = dirname($ScriptBase)."/img"; 

// Replace following emoticons (in regular expression) to image files
$ReplaceEmoticons = array(
    ":-)" => "$ImgDir/icon_smile.gif",
    ":-(" => "$ImgDir/icon_sad.gif",  
    ":P"  => "$ImgDir/icon_grimace.gif", 
    "XD"  => "$ImgDir/icon_xd.gif",        
    "=.=" => "$ImgDir/icon_closeeye.gif",
    "^.^" => "$ImgDir/icon_happy.gif" 
  );

Modify the emoticons and filenames as you wish. Keys(left-hand side) are what text to be replaced. Be sure to use the terms as less occuring in ordinary articles as you can. Values(right-hand side) are filenames to be linked to the corresponding icons.

./lib/defaults.php

Modify variable $ParseEngine, adding 'parse_emoticon', as below. $ParseEngine is ORDER-sentitive.

$ParseEngine = array(
//                 'parse_raw_html',
                 'parse_htmlisms',
                 'parse_nowiki',
                 'parse_hyperlink_ref',
                 'parse_hyperlink_description',
                 'parse_hyperlink',
                 'parse_macros',
                 'parse_transclude',
                 'parse_freelink',
                 'parse_interwiki',
                 'parse_wikiname',
                 'parse_textenhance',
                 'parse_bold',
                 'parse_italic',
                 'parse_teletype',
                 'parse_heading',
                 'parse_table',
                 'parse_horiz',
                 'parse_indents',
                 'parse_newline',
                 'parse_emoticon',  // Add this line: show graphic emoticon
                 'parse_elements'
               );

Modify variable $DisplayEngine, adding the line as below. $DisplayEngine is ORDER-insentitive.

$DisplayEngine = array(
                   'emoticon'         => 'html_emoticon', // Add this line
./lib/default.php wrote: DO NOT EDIT THIS FILE. However, modifying $ParseEngine and $DisplayEngine in ./config.php is space-consuming. Is there any alternative way? --MiGi

./parse/transforms.php

Add the following function:

function parse_emoticon($text)
{
  global $ReplaceEmoticons;
  static $FirstTime=true; 
  static $patterns, $replacements; // remember the patterns and replacements 
                                   // so that it won't be generated again.
  
  // the following procedure is just done once
  if ($FirstTime) {
      $i=0;
      foreach ($ReplaceEmoticons as $key => $val)
      {
            $patterns[$i] = $key;
            $replacements[$i] = new_entity(array('emoticon',$val));
            $i++;
      }
      $FirstTime = false;
  }
  
  if (count($patterns))
      return str_replace($patterns, $replacements, $text);
  else 
      return $text;
}

./parse/html.php

Add the following function:

function html_emoticon($filename) 
{
    return "<img src=\"$filename\" alt=\"$filename\"/>";
}

Authors

MiGi, ernest, Bob Chao