Return to Snippet

Revision: 60158
at October 24, 2012 05:23 by martijnhoogwerf


Initial Code
<?php
     
    function emoticons($text) {
        $emoticons = array(
        '<img src="images/smiley-happy.png" alt="emoticon" />' => array('1' => ':-)', '2' => ':)'),
        '<img src="images/smiley-wink.png" alt="emoticon" />' => array('1' => ';-)', '2' => ';)'),
        '<img src="images/smiley-cool.png" alt="emoticon" />' => array('1' => '8)', '2' => '8|')
        );
        foreach ($emoticons as $key => $emoticon) {
            $text = str_replace($emoticon, $key, $text);
        }
        return $text;
    }
     
    echo emoticons('This is :) a text ;-) with :-) emoticons 8)');
     
?>

Initial URL
http://www.flashstar.nl/article-37.html

Initial Description
If you are a PHP developer, you are probably aware that you can pass an array of strings as the search strings and an array as the replacements to the str_replace function. In this example you can see how to automatically replace smiley shortcuts by an image.

Initial Title
PHP - Generating Emoticons

Initial Tags
php

Initial Language
PHP