PHP - Generating Emoticons


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. function emoticons($text) {
  4. $emoticons = array(
  5. '<img src="images/smiley-happy.png" alt="emoticon" />' => array('1' => ':-)', '2' => ':)'),
  6. '<img src="images/smiley-wink.png" alt="emoticon" />' => array('1' => ';-)', '2' => ';)'),
  7. '<img src="images/smiley-cool.png" alt="emoticon" />' => array('1' => '8)', '2' => '8|')
  8. );
  9. foreach ($emoticons as $key => $emoticon) {
  10. $text = str_replace($emoticon, $key, $text);
  11. }
  12. return $text;
  13. }
  14.  
  15. echo emoticons('This is :) a text ;-) with :-) emoticons 8)');
  16.  
  17. ?>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.