我正在开发简单的聊天应用程序,我想在聊天窗口中添加情绪(笑脸)。但我不知道该怎么做,谁能帮我
像这样
谢谢
尝试这个
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);
}
为“:-)”之类的表情符号创建一组代码,并具有一个转换数组,例如。
$smile_keys = array(':-)',...,...etc)
$smile_images = array('face1.jpg',...,...etc);
echo str_replace($smile_keys, $smile_images, $text);
这是一种看起来相当简单的方法。