在HTML中,中文的“好好學(xué)習(xí)”可以表示為“好好學(xué)習(xí)”
在項(xiàng)目中,需要對(duì)接短信告警,短信告警返回?cái)?shù)據(jù)要求是utf8的
后來繼續(xù)溝通,才發(fā)現(xiàn)要的是html-utf8 的;
沒有找到合適的golang工具包,涉及語(yǔ)言轉(zhuǎn)碼的包主要有
mahonia,支持各種格式的數(shù)據(jù)轉(zhuǎn)換,gbk,utf8,gb2312
net/html,支持網(wǎng)頁(yè)轉(zhuǎn)碼,改的是轉(zhuǎn)換網(wǎng)頁(yè)中的<,>,&,',',.
根據(jù)網(wǎng)上經(jīng)驗(yàn),提供三個(gè)版本的轉(zhuǎn)換:
Javascript
function ConvUtf8(obj) {returnobj.replace(/[^/u0000-/u00FF]/g,function($0) {returnescape($0).replace(/(%u)(/w{4})/gi, "&#x$2;") });}
JAVA
public static String UTF8_html_conv(String str){StringBuffer stbPreemptionArg = new StringBuffer();for(int i = 0;i<str.length();i++){if (str.codePointAt(i) > 255){stbPreemptionArg.append("&#x"+Integer.toString(str.charAt(i), 16)+";");}else{stbPreemptionArg.append(str.charAt(i));}}return stbPreemptionArg.toString();}
Golang
func CovertToHtml(src string) string{ rs := []rune(src) htmlUtf8 := "" for _, r := range rs { rint := int(r) if rint < 128 { htmlUtf8 += string(r) } else { //utf8 = "//u"+strconv.FormatInt(int64(rint), 16) htmlUtf8 += "&#x"+strconv.FormatInt(int64(rint), 16) + ";" } }return htmlUtf8}
總結(jié)
以上所述是小編給大家介紹的中文轉(zhuǎn)換成html中的utf-8的方法,希望對(duì)大家有所幫助!
新聞熱點(diǎn)
疑難解答