将表情符号转换为自定义图像

时间:2015-12-31 21:38:33

标签: php string utf-8 utf emoticons

输入 - 嘿我笑了

输出 - 嘿我笑了<span class ="smile"></span>

代码

$emoticons = array('' =>'<span class ="smile"></span>') ;
$str = strtr($str, $emoticons) ;

我无法使用str_replace,因为$emoticons数组中有多个元素。

以上代码无效,输入和输出保持不变。

1 个答案:

答案 0 :(得分:1)

这对我有用:

<?php
$str = "hey I'm smiling  and I'm crying  "; // input
$emoticons = array('' =>'<span class="smile"></span>','' =>'<span class="cry"></span>') ; // array of emoticons and spans
$output = strtr($str, $emoticons); // change emoticons from array to spans from array
echo $output; // print it
?>