js转码与解码emoji山东旅游景点大全
javascript正常的英⽂编码是utf-8的,mysql默认存的也是这种编码,⽽emoji表情是utf-16的,这就导致了db存储emoji会有问题,所以最好的⽅式是,把emoji先转成utf-8的这种
实体编码,存到数据库⾥,要使⽤的时候,从db拿出来,再解码成utf-16的形式。
{
// 表情转码
utf16toEntities(str) {
const patt = /[\ud800-\udbff][\udc00-\udfff]/g; // 检测utf16字符正则
str = place(patt, (char) => {
let H;
如何修身let L;
let code;
let s;
if (char.length === 2) {
H = char.charCodeAt(0); // 取出⾼位
L = char.charCodeAt(1); // 取出低位
code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00; // 转换算法
s = `&#${code};`;
} el {
s = char;
}
return s;
});
return str;
},
// 表情解码
entitiestoUtf16(strObj) {
const patt = /&#\d+;/g;
const arr = strObj.match(patt) || [];
let H;
let L;
如何给u盘加密
let code;
for (let i = 0; i < arr.length; i += 1) {
code = arr[i];
code = place('&#', '').replace(';', '');
// ⾼位
H = Math.floor((code - 0x10000) / 0x400) + 0xD800;
// 低位
L = ((code - 0x10000) % 0x400) + 0xDC00;
code = `&#${code};`;
const s = String.fromCharCode(H, L);
strObj = place(code, s);
}
return strObj;
}
}
使⽤⽰例
const s = 'test emoji ';
const dbSaveStr = utf16toEntities(s);
// 结果是: 'test emoji 👇👉👈🙌' 这样⼦的实体编码字符串存到db就没问题了
// 要使⽤时,想数据库中拿到上⾯的存储记录
cosnt dbOutStr = 'test emoji 👇👉👈🙌' ;
// 然后将其中的emoji转码成表情使⽤
const ret = entitiestoUtf16(dbOutStr )
方言笑话
// 得到: 'test emoji ';
读万卷书行万里路是谁写的
将emoji转成UTF-16
这是更通⽤的⽅式,因为上⾯那种只是在web端能显⽰,如果是要存到db,给c++获取数据再客户端app展⽰,就⾏不通了。必须转成unicode-16才⾏。写真摄影艺术
// /2013/09/javascript-unicode.html
function toUTF16(codePoint) {
var TEN_BITS = parInt('1111111111', 2);
function u(codeUnit) {
return '\\u'+String(16).toUpperCa();
}
if (codePoint <= 0xFFFF) {
return u(codePoint);
}
codePoint -= 0x10000;
// Shift right to get to most significant 10 bits
var leadSurrogate = 0xD800 + (codePoint >> 10);
// Mask to get least significant 10 bits
var tailSurrogate = 0xDC00 + (codePoint & TEN_BITS);
return u(leadSurrogate) + u(tailSurrogate);
}
使⽤⽰例
// using codePointAt, it's easy to go from emoji
// to decimal and back.
// Emoji to decimal reprentation
" ".codePointAt(0)
>128512
// Decimal to emoji
怎么才能祛斑
String.fromCodePoint(128512)
>" "
// going from emoji to hexadecimal is a little
// bit trickier. To convert from decimal to hexadecimal,
// we can u toUTF16.
// Decimal to hexadecimal
toUTF16(128512)
> "\uD83D\uDE00"
// Hexadecimal to emoji
"\uD83D\uDE00"
> " "
上海市社保官网判断字符串是否为emoji字符
const emojiReg = /(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe23\u20d0-\u20f0]|\ud83c[\udffb-\udfff])?(?:\u200d(?:[^\ud800-\udfff]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff]