如何在 PHP 代码中插入笑脸?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2764282/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Insert Smileys in PHP Code?
提问by Somon
I have a Shoutout Box written in PHP language.It doesnt have Smileys Support. How Can I Insert Smiley Support in it?
我有一个用 PHP 语言编写的 Shoutout Box。它没有 Smileys 支持。如何在其中插入笑脸支持?
回答by Day
Some PHP that worked for me back in the day ;)
一些当时对我有用的 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);
}


回答by Micha? Mech
You can simply do:
你可以简单地做:
<?php
echo str_replace(';)', '<img src="path/to/smile_image.gif" title=";)"/>', $message);
?>
回答by Rob
I would use javascript to check added shouts for combinations like ':-)' and replace them with an image of an smiley
我会使用 javascript 来检查添加的呼喊声是否包含像“:-)”这样的组合,并用笑脸图像替换它们
回答by Imran Qamer
I found this and it helped me.. http://os.alfajango.com/css-emoticons/
我发现了这个,它帮助了我.. http://os.alfajango.com/css-emoticons/
回答by Nisarg Bhavsar
Create function with pass string. And replace with the some text to image like below.
使用传递字符串创建函数。并替换为如下所示的一些文本图像。
function parseString($string ) {
$my_smilies = array(
    ':aln' => '<img src="images/alien1.png" alt="" />',
    ':thk' => '<img src="images/annoyed.png" alt="" />',
    ':ang' => '<img src="images/angel.png" alt="" />',
    ':slp<' => '<img src="images/zzz.png" alt="" />',
    ':blnk' => '<img src="images/blanco.png" alt="" />',
    ':zip' => '<img src="images/zip_it.png" alt="" />',
    ':bor' => '<img src="images/boring.png" alt="" />',
);
return str_replace( array_keys($my_smilies), array_values($my_smilies), $string);
}
}

