网页编码(网页的编码方式)

lxf2023-03-15 16:43:01

汉字全部转换成html实体(十进制Unicode编码)。这样做的好处是,无论网页编码如何,都可以正常显示汉字,不会出现乱码。当然也适用于其他字符集。

在php中,我们可以使用mbstring的mb_convert_encoding函数来实现这种正反向转换。
例如:

mb_convert_encoding ("“你好,”& quotHTML-实体& quot,& quotgb2312 & quot);//输出:& amp # 20320& amp#22909;
MB _ convert _ encoding(& quot;& amp#20320;& amp#22909;",& quotgb2312 & quot,& quotHTML-实体& quot);//Output: Hello

如果需要转换整个页面,只需要在php文件的开头添加这三行代码:
MB _ internal _ encoding(" GB 2312 & quot;);//这里的gb2312是你网站的原代码
MB _ http _ output(" HTML-ENTITIES & quot;);
ob _ start(& # 39;mb _ output _ handler & # 39);

Asp版本可以使用下面的函数来实现这种转换:

Function html entities(str)
For I = 1 to Len(str)
char = mid(str,I,1)
If AscW(char)& gt;0,则
html entities = html entities & amp;"& amp# & quot& ampAscw(char)和amp";"
Else
html entities = html entities & amp;"& amp# & quot& amp(65536+ascW(char))& amp;";"
end if
next
end function

JS版本

[/br]& quot;;
(I = 0;我& lt字符串长度;i++)
{
temp = str . charcode at(I);
r+= & quot;& amp# & quot+temp+& quot;;";
}

//也可以用正则表达式
/r = str求解。替换(/[\ d \ d & amp;# & quot+$ 0 . charcodeat(0)+& quot;;";});
return r;
}

ASP。NET (C #)版
Private String get html Entities(String STR)
[/h]
for(int I = 0;我& lt海峡。长度;i++)
{
r+= & quot;& amp# & quot+Char。convert tout 32(str,I)+& quot;;";
}
return r;
}

相关文档:网页中常用的HTML字符实体。

以上是HTML实体和网页编码的细节。更多详情请关注AdminJS的其他相关文章!