如何在PHP代码中插入表情符号?

Som*_*mon 5 php chat

我有一个用PHP语言编写的Shoutout Box.它没有Smileys支持.如何在其中插入笑脸支持?

Day*_*Day 16

一些PHP在当天为我工作 ;)

function Smilify(&$subject)
{
    $smilies = array(
        ':|'  => 'mellow',
        ':-|' => 'mellow',
        ':-o' => 'ohmy',
        ':-O' => 'ohmy',
        ':o'  => 'ohmy',
        ':O'  => 'ohmy',
        ';)'  => 'wink',
        ';-)' => 'wink',
        ':p'  => 'tongue',
        ':-p' => 'tongue',
        ':P'  => 'tongue',
        ':-P' => 'tongue',
        ':D'  => 'biggrin',
        ':-D' => 'biggrin',
        '8)'  => 'cool',
        '8-)' => 'cool',
        ':)'  => 'smile',
        ':-)' => 'smile',
        ':('  => 'sad',
        ':-(' => 'sad',
    );

    $sizes = array(
        'biggrin' => 18,
        'cool' => 20,
        'haha' => 20,
        'mellow' => 20,
        'ohmy' => 20,
        'sad' => 20,
        'smile' => 18,
        'tongue' => 20,
        'wink' => 20,
    );

    $replace = array();
    foreach ($smilies as $smiley => $imgName)
    {
        $size = $sizes[$imgName];
        array_push($replace, '<img src="imgs/'.$imgName.'.gif" alt="'.$smiley.'" width="'.$size.'" height="'.$size.'" />');
    }
    $subject = str_replace(array_keys($smilies), $replace, $subject);
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 这是我在这个网站上看到的最好的答案之一.大量竖起大拇指. (4认同)

Mic*_*ech 7

你可以简单地做:

<?php
echo str_replace(';)', '<img src="path/to/smile_image.gif" title=";)"/>', $message);
?>
Run Code Online (Sandbox Code Playgroud)


Rob*_*Rob 1

我会使用 javascript 来检查添加的喊声是否有“:-)”之类的组合,并将其替换为笑脸图像