您的位置:首页 > Web前端 > HTML

网页中的字符编码(html的unicode实体编码)

2012-02-04 21:11 190 查看

1、编码转换(to Unicode)

(程序代码来源于网络)

Js版

<script>
test = "你好abc"
str = ""
for( i=0;     i<test.length; i++ )
{
temp = test.charCodeAt(i).toString(16);
str     += "\\u"+ new Array(5-String(temp).length).join("0") +temp;
}
document.write (str)
</script>


vbs版

Function Unicode(str1)
Dim str,temp
str = ""
For i=1     to len(str1)
temp = Hex(AscW(Mid(str1,i,1)))
If len(temp) < 5 Then     temp = right("0000" & temp, 4)
str = str & "\u" & temp
Next
Unicode = str
End Function


Function htmlentities(str)
For i = 1 to Len(str)
char = mid(str, i, 1)
If Ascw(char) > 128 then
htmlentities = htmlentities & "&#" & Ascw(char) & ";"
Else
htmlentities = htmlentities & char
End if
Next
End Function


coldfusion版

function nochaoscode(str)
{
var new_str = “”;
for(i=1; i lte len(str);i=i+1){
if(asc(mid(str,i,1)) lt 128){
new_str = new_str & mid(str,i,1);
}else{
new_str = new_str & “&##” & asc(mid(str,i,1));
}
}
return new_str;
}


附:

在php中我们可以用mbstring的mb_convert_encoding函数实现这个正向及反向的转化。 如:

mb_convert_encoding ("你好", "HTML-ENTITIES", "gb2312"); //输出:你好
mb_convert_encoding ("你好", "gb2312", "HTML-ENTITIES"); //输出:你好

如果需要对整个页面转化,则只需要在php文件的头部加上这三行代码:

mb_internal_encoding("gb2312"); // 这里的gb2312是你网站原来的编码
mb_http_output("HTML-ENTITIES");
ob_start('mb_output_handler');

如果没有打开mbstring扩展,可以参考coolcode.cn上的这两篇文章:
在任意字符集下正常显示网页的方法
在任意字符集下正常显示网页的方法(续)

2、HTML实体

HTML 4.01 支持 ISO 8859-1 (Latin-1) 字符集。

提示 实体名是区分大小写的。

备注 同一个符号,可以用“实体名称”和“实体编号”两种方式引用,“实体名称”的优势在于便于记忆,但不能保证所有的浏览器都能顺利识别它,而“实体编号”则没有这种担忧,但它实在不方便记忆。

[align=center][/align]
ASCII中部分实体的新名字

显示

描述

实体名称

实体编号

"quotation mark

""
'apostrophe' (IE下无效)

'
&ampersand&&
<less-than<<
>greater-than>>
ISO 8859-1 符号实体

显示

描述

实体名称

实体编号

non-breaking space

  
¡inverted exclamation mark

¡¡
¤currency¤¤


cent¢¢


pound££


yen¥¥
¦broken vertical bar

¦¦
§section§§
¨spacing diaeresis

¨¨
©copyright©©
afeminine ordinal indicator

ªª
«angle quotation mark (left)

««
?negation¬¬
-soft hyphen

­­
®registered trademark

®®
trademark
ˉspacing macron

¯¯
°degree°°
±plus-or-minus±±
2superscript 2

²²
3superscript 3

³³
spacing acute

´

´
μmicroµµ
?paragraph
·middle dot

··
?spacing cedilla

¸¸
1superscript 1

¹¹
omasculine ordinal indicator

ºº
»angle quotation mark (right)

»»
?fraction 1/4

¼¼
?fraction 1/2

½½
?fraction 3/4

¾¾
?inverted question mark

¿¿
×multiplication××
÷division÷÷
ISO 8859-1 字符实体

显示

描述

实体名称

实体编号

Àcapital a, grave accent

ÀÀ
Ácapital a, acute accent

ÁÁ
Âcapital a, circumflex accent

ÂÂ
Ãcapital a, tilde

ÃÃ
Äcapital a, umlaut mark

ÄÄ
Åcapital a, ring

ÅÅ
Æcapital ae

ÆÆ
Çcapital c, cedilla

ÇÇ
Ècapital e, grave accent

ÈÈ
Écapital e, acute accent

ÉÉ
Êcapital e, circumflex accent

ÊÊ
Ëcapital e, umlaut mark

ËË
Ìcapital i, grave accent

ÌÌ
Ícapital i, acute accent

ÍÍ
Îcapital i, circumflex accent

ÎÎ
Ïcapital i, umlaut mark

ÏÏ
Ðcapital eth, Icelandic

ÐÐ
Ñcapital n, tilde

ÑÑ
Òcapital o, grave accent

ÒÒ
Ócapital o, acute accent

ÓÓ
Ôcapital o, circumflex accent

ÔÔ
Õcapital o, tilde

ÕÕ
Öcapital o, umlaut mark

ÖÖ
Øcapital o, slash

ØØ
ùcapital u, grave accent

ÙÙ
úcapital u, acute accent

ÚÚ
?capital u, circumflex accent

ÛÛ
ücapital u, umlaut mark

ÜÜ
Ycapital y, acute accent

ÝÝ
Tcapital THORN, Icelandic

ÞÞ
?small sharp s, German

ßß
àsmall a, grave accent

àà
ásmall a, acute accent

áá
asmall a, circumflex accent

ââ
?small a, tilde

ãã
?small a, umlaut mark

ää
?small a, ring

åå
?small ae

ææ
?small c, cedilla

çç
èsmall e, grave accent

èè
ésmall e, acute accent

éé
êsmall e, circumflex accent

êê
?small e, umlaut mark

ëë
ìsmall i, grave accent

ìì
ísmall i, acute accent

íí
?small i, circumflex accent

îî
?small i, umlaut mark

ïï
esmall eth, Icelandic

ðð
?small n, tilde

ññ
òsmall o, grave accent

òò
ósmall o, acute accent

óó
?small o, circumflex accent

ôô
?small o, tilde

õõ
?small o, umlaut mark

öö
?small o, slash

øø
ùsmall u, grave accent

ùù
úsmall u, acute accent

úú
?small u, circumflex accent

ûû
üsmall u, umlaut mark

üü
ysmall y, acute accent

ýý
tsmall thorn, Icelandic

þþ
?small y, umlaut mark

ÿÿ
其它一些 HTML 所支持的实体

显示

描述

实体名称

实体编号

Œcapital ligature OE

ŒŒ
œsmall ligature oe

œœ
Šcapital S with caron

ŠŠ
šsmall S with caron

šš
Ÿcapital Y with diaeres

ŸŸ
ˆmodifier letter circumflex accent

ˆˆ
˜small tilde

˜˜


en space



em space



thin space

zero width non-joiner

zero width joiner

left-to-right mark

right-to-left mark

en dash

em dash

left single quotation mark

right single quotation mark

single low-9 quotation mark

left double quotation mark

right double quotation mark

double low-9 quotation mark

dagger
double dagger

horizontal ellipsis

per mille

single left-pointing angle quotation

single right-pointing angle quotation

euro
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: