How to convert smiley codes to smiley images in web page using php
How to convert smiley codes to smiley images in web page using php
When I was working on a chat application, I had gone through this problem where user uses smiley codes in chat but it changes to smiley image. This kind of chat smiley conversion can be seen in Facebook. Making this kind of smiley chat system not typical. You only need to identify the smiley code and then convert it into image code.
Look at the code below which has some functions.
<?php function convertEmoticons($body) { $emoticons = array( "O:)" => "angel", ":X" => "angry", ":S" => "confused", ";(" => "crying", ":$" => "embarrassed", "8)" => "glasses", ":-]" => "grin", ":*" => "kiss", ":|" => "plain", "8|" => "rolling", ":(" => "sad", "+(" => "sick", ":)" => "smile", ":D" => "smile-big", ":O" => "surprise", ":p" => "tongue", ":?" => "uh", ";)" => "wink", "=)" => "devil", ); $emoticonReplaceCount = 0; foreach ($emoticons as $asciiCode => $imageName) { while (strstr($body, $asciiCode) and $emoticonReplaceCount < 10) { $body = substr_replace( $body, '<img src="emoticons/'.$imageName.'.gif" align="absbottom" width="20" height="20" alt="'.$imageName.'" title="'.$imageName.'" >', strpos($body, $asciiCode, 0), strlen($asciiCode) ); $emoticonReplaceCount ++; } } return $body; } function printEmoticons() { $emoticons = array( "O:)" => "angel", ":X" => "angry", ":S" => "confused", ";(" => "crying", ":$" => "embarrassed", "8)" => "glasses", ":-]" => "grin", ":*" => "kiss", ":|" => "plain", "8|" => "rolling", ":(" => "sad", "+(" => "sick", ":)" => "smile", ":D" => "smile-big", ":O" => "surprise", ":p" => "tongue", ":?" => "uh", ";)" => "wink", "=)" => "devil", ); echo('<table width=100%><tr>'); $i=0; foreach ($emoticons as $asciiCode => $imageName) { if($i%5==0) { echo('</tr><tr>'); } echo('<td>'.$asciiCode.'</td><td><img src="emoticons\'.$imageName.'.gif" align="absbottom" width="20" height="20" alt="'.$imageName.'" title="'.$imageName.'"></td>'); $i++; } } ?>
The code given above will change the smiley code to smiley image. You have to show this to your webpage.
Leave a comment
Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.