Posted By

martijnhoogwerf on 10/24/12


Tagged

php emoticons strreplace


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

pixelsoul


PHP - Generating Emoticons


 / Published in: PHP
 

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

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.

  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. ?>

Report this snippet  

You need to login to post a comment.