Home > Backend Development > PHP Tutorial > javascript - 如何把字符在前端转成QQ表情?

javascript - 如何把字符在前端转成QQ表情?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:29:00
Original
1764 people have browsed it

就是前端输出一段诸如/:8-)之类的代码,
然后在前端把字符转成相应的QQ表情,请问有大神知道怎么样可以实现吗?

回复内容:

就是前端输出一段诸如/:8-)之类的代码,
然后在前端把字符转成相应的QQ表情,请问有大神知道怎么样可以实现吗?

既然是 代码表情,那么肯定会有一个代码与表情(图片地址)的映射吧!

$array = array(
    '/:8-' => '<img  src="/static/imghw/default1.png"  data-src="/path/to/face-1.jpg"  class="lazy" / alt="javascript - 如何把字符在前端转成QQ表情?" >',
    '/:9-' => '<img  src="/static/imghw/default1.png"  data-src="/path/to/face-2.jpg"  class="lazy" / alt="javascript - 如何把字符在前端转成QQ表情?" >',
    '/:10-' => '<img  src="/static/imghw/default1.png"  data-src="/path/to/face-3.jpg"  class="lazy" / alt="javascript - 如何把字符在前端转成QQ表情?" >',
);

$comment = '你好啊 /:8-,你在哪里呢/:10-?';

echo strtr($comment, $array);
Copy after login

结果:

你好啊 <img  src="/static/imghw/default1.png"  data-src="/path/to/face-1.jpg"  class="lazy" / alt="javascript - 如何把字符在前端转成QQ表情?" >,你在哪里呢 <img  src="/static/imghw/default1.png"  data-src="/path/to/face-3.jpg"  class="lazy" / alt="javascript - 如何把字符在前端转成QQ表情?" >?
Copy after login

正则或者字符串替换替换成img图片地址。。。

function replace_em(str){

<code>str = str.replace(/\</g,'<');
str = str.replace(/\>/g,'>');
str = str.replace(/\n/g,'<br/>');
str = str.replace(/\[em_([0-9]*)\]/g,'<img  src="/static/imghw/default1.png"  data-src="face/$1.gif"  class="lazy" border="0"   style="max-width:90%" / alt="javascript - 如何把字符在前端转成QQ表情?" >');

str = str.replace(/\[img_([\d\D]*)\]/g,'<img  src="/static/imghw/default1.png"  data-src="upload/$1"  class="lazy" border="0"   style="max-width:90%" / alt="javascript - 如何把字符在前端转成QQ表情?" ><a href="upload/$1" target="_blank"></a>');

return str;</code>
Copy after login

}

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template