A better way to replace emoticons in PHP?

后端 未结 5 1528
忘掉有多难
忘掉有多难 2020-12-05 21:40

Right now I am using this function for emoticons:

function emoticons($text) {
        $icons = array(
                \':)\'    =>  \'

        
相关标签:
5条回答
  • 2020-12-05 22:04

    I'm not sure whether it will work or not, but I'd try to put some extra spaces like this:

    function emoticons($text) {
        $icons = array(
                ' :) '    =>  ' <img src="/images/blank.gif" alt="smile" class="icon_smile" /> ',
                ' :-) '   =>  ' <img src="/images/blank.gif" alt="smile" class="icon_smile" /> ',
                ' :D '    =>  ' <img src="/images/blank.gif" alt="smile" class="icon_laugh" /> ',
                ' :d '    =>  ' <img src="/images/blank.gif" alt="laugh" class="icon_laugh" /> ',
                ' ;) '    =>  ' <img src="/images/blank.gif" alt="wink" class="icon_wink" /> ',
                ' :P '    =>  ' <img src="/images/blank.gif" alt="tounge" class="icon_tounge" /> ',
                ' :-P '   =>  ' <img src="/images/blank.gif" alt="tounge" class="icon_tounge" /> ',
                ' :-p '   =>  ' <img src="/images/blank.gif" alt="tounge" class="icon_tounge" /> ',
                ' :p '    =>  ' <img src="/images/blank.gif" alt="tounge" class="icon_tounge" /> ',
                ' :( '    =>  ' <img src="/images/blank.gif" alt="sad face" class="icon_sad" /> ',
                ' :o '    =>  ' <img src="/images/blank.gif" alt="shock" class="icon_shock" /> ',
                ' :O '    =>  ' <img src="/images/blank.gif" alt="shock" class="icon_shock" /> ',
                ' :0 '    =>  ' <img src="/images/blank.gif" alt="shock" class="icon_shack" /> ',
                ' :| '    =>  ' <img src="/images/blank.gif" alt="straight face" class="icon_straight" /> ',
                ' :-| '   =>  ' <img src="/images/blank.gif" alt="straight face" class="icon_straight" /> ',
                ' :/ '    =>  ' <img src="/images/blank.gif" alt="straight face" class="icon_straight" /> ',
                ' :-/ '   =>  ' <img src="/images/blank.gif" alt="straight face" class="icon_straight" /> '
        );
        return strtr($text, $icons);
    }
    
    0 讨论(0)
  • 2020-12-05 22:17

    I think using regular expressions instead of just defining your emoticons. This could look like

    // replaces a :) when it is surrounded by whitespaces
    return preg_replace('/\s:)\s/', '<img ...>', $text);
    
    0 讨论(0)
  • 2020-12-05 22:21

    Regular expressions are slow for this if the text is big, as much as i like them, you should only insert an icon if it's surrounded by spaces, that way your users won't riot when trying to write urls.

    0 讨论(0)
  • 2020-12-05 22:24

    I'd recommend two libraries:

    Emojify: https://github.com/HallgrenNetworks/emojify

    PHP Emoji: https://github.com/iamcal/php-emoji

    These will deal with these cases for you.

    0 讨论(0)
  • 2020-12-05 22:25

    You can use the preg_replace function and then use word boundaries in the regular expression.

     foreach($icons as $icon=>$image) {
          $icon = preg_quote($icon);
          $text = preg_replace("~\b$icon\b~",$image,$text);
     }
    

    You need to use word boundaries and not white space because this will take care of the start and end points to. Needing a space before means that just a :) won't be found.

    0 讨论(0)
提交回复
热议问题