加入收藏 | 设为首页 | 会员中心 | 我要投稿 焦作站长网 (https://www.0391zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 安全 > 正文

彻底解决页面文字编码乱码问题

发布时间:2020-03-16 09:26:56 所属栏目:安全 来源:互联网
导读:在HTML页面中、Javascript脚本中、以及XML数据传输中,因为编码导致的问题不计其数。


function htmlEncode($text)
{
$encoded = "";
for( $index = 0; $index < strlen($text); $index++)
{
if( ord($text[$index]) <= 127 )
{
switch(ord($text[$index]))
{
case 34:
case 38:
case 39:
case 60:
case 62:
$encoded .= "&#" .ord($text[$index]).";";
break;
default:
$encoded .= $text[$index];
}
}
else
{
$char = $text[$index] . $text[$index+1];
$char = mb_convert_encoding( $char, "utf-16", "gbk");
$encoded .= "&#" . (ord($char[0])*256 + ord($char[1])) . ";";
$index++;
}
}
return $encoded;
}
function htmlEncode($text)
{
$encoded = "";
for( $index = 0; $index < strlen($text); $index++)
{
if( ord($text[$index]) <= 127 )
{
switch(ord($text[$index]))
{
case 34:
case 38:
case 39:
case 60:
case 62:
$encoded .= "&#" .ord($text[$index]).";";
break;
default:
$encoded .= $text[$index];
}
}
else
{
$char = $text[$index] . $text[$index+1];
$char = mb_convert_encoding( $char, "utf-16", "gbk");
$encoded .= "&#" . (ord($char[0])*256 + ord($char[1])) . ";";
$index++;
}
}
return $encoded;
}


然后通过XML返回,这样就能够彻底杜绝乱码。

复制代码 代码如下:

(编辑:焦作站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读