Search code examples
phpstringutf-8utfemoticons

Convert emoji into custom images


Input - hey I'm smiling 😀

Output - hey I'm smiling <span class ="smile"></span>

Code

$emoticons = array('😀' =>'<span class ="smile"></span>') ;
$str = strtr($str, $emoticons) ;

I can't use str_replace because I have more than one element in $emoticons array.

This above code is not working the input and output remains same.


Solution

  • This works for me:

    <?php
    $str = "hey I'm smiling 😀 and I'm crying 😢 😢"; // input
    $emoticons = array('😀' =>'<span class="smile"></span>','😢' =>'<span class="cry"></span>') ; // array of emoticons and spans
    $output = strtr($str, $emoticons); // change emoticons from array to spans from array
    echo $output; // print it
    ?>