在PHP中替换表情符号的更好方法? [英] A better way to replace emoticons in PHP?

查看:332
本文介绍了在PHP中替换表情符号的更好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在使用此功能来表情:

Right now I am using this function for emoticons:

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);
    }

但是,如果您输入链接,则http://:/部分将被替换:

But if you enter a link the :/ part of http:// gets replaced:

与代码相同(每行前面有4个空格以创建代码框),但由于某种原因,一个简单的)也被替换(用眨眼):

Same with code (4 spaces in front of each line to create code box), but for some reason a simple ) gets replaced (with a wink) as well:

有没有比这样替换所有图标更好的解决方案?显然效果不好.我在Google上进行了很多搜索,但没有找到更好的方法.我还尝试过在上面的关联数组中的每个表情符之前放置一个空格,但是如果您只输入:)却没有任何空格,那将不起作用.

Is there a better solution to this than replacing all of the icons like this? It obviously doesn't work good. I have searched on Google a lot and haven't found any better way. I also tried putting a space before each emoticon in the associative array above but that doesn't work if you just enter a :) without anything before it.

推荐答案

您可以使用

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.

这篇关于在PHP中替换表情符号的更好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆