PHP编码转换函数 自动转换字符集支持数组转换
时间:2024-05-02 17:33:28
// 自动转换字符集 支持数组转换
function auto_charset($fContents, $from='gbk', $to='utf-8') {
$from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
$to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
if (strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents))) {
//如果编码相同或者非字符串标量则不转换
return $fContents;
}
if (is_string($fContents)) {
if (function_exists('mb_convert_encoding')) {
return mb_convert_encoding($fContents, $to, $from);
} elseif (function_exists('iconv')) {
return iconv($from, $to, $fContents);
} else {
return $fContents;
}
} elseif (is_array($fContents)) {
foreach ($fContents as $key => $val) {
$_key = auto_charset($key, $from, $to);
$fContents[$_key] = auto_charset($val, $from, $to);
if ($key != $_key)
unset($fContents[$key]);
}
return $fContents;
}
else {
return $fContents;
}
}
当我们在接受未知客户端提交的数据,由于各客户端的编码不统一,但在我们的服务器端最终只能以一种编码方式来处理,这种情况下就会涉及到一个将接受到的字符转换为特定编码的问题。
这时可能会想到直接用iconv来进行转码,但我们知道,iconv这个函数需要提供的两个参数为输入编码和输出编码,而我们现在根本不知道接受的字符串是什么编码,如果这个时候能得到接收字符是什么编码就好了。
对于这样的问题,一般会有两种解决方案。
方案一
要客户端提交数据时,指定所提交的编码,这时就需要多给一个用来指定编码的变量。
$string = $_GET['charset'] === 'gbk' ? iconv('gbk','utf-8',$_GET['str']) : $_GET['str'];
对于这种情况,如果在没有约定或者我们不能控制客户端的情况下,似乎这种方案使用不是很好。
方案二
直接由服务器端来检测所接收的数据编码。
这种方案当然是最理想了的了,现在问题是怎么检测一个字符的编码吗?对于这种情况,在php里,mb_string这个扩展中的mb_check_encoding提供了我们所需要的功能。
$str = mb_check_encoding($_GET['str'],'gbk') ? iconv('gbk','utf-8',$_GET['str']) : $_GET['str'];
但这需要打开mb_string这个扩展,有些时候可能我们的生产服务器中没有打开这个扩展。对于这种情况,需要自己借助如下函数来判断编码。
以下函数非本人所写
function isGb2312($string) {
for($i=0; $i 127) {
if( ($v >= 228) && ($v < = 233) )
{
if( ($i+2) >= (strlen($string) - 1)) return true;
$v1 = ord( $string[$i+1] );
$v2 = ord( $string[$i+2] );
if( ($v1 >= 128) && ($v1 < =191) && ($v2 >=128) && ($v2 < = 191) )
return false;
else
return true;
}
}
}
return true;
}
function isUtf8($string) {
return preg_match('%^(?:
[\x09\x0A\x0D\x20-\x7E] # ASCII
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)*$%xs', $string);
}
这里我们就可以使以上任何一个函数来实现编码的检测。并将其转换成指定的编码。
$str = isGb2312($_GET['str'],'gbk') ? iconv('gbk','utf-8',$_GET['str']) : $_GET['str'];
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Dreamweaver技巧十二招
在Python中合并字典模块ChainMap的隐藏坑【推荐】
![](https://img.aspxhome.com/file/2023/0/127920_0s.png)
vuex actions异步修改状态的实例详解
MySQL数据库开启、关闭、查看函数功能的方法
vue中jsonp插件的使用方法示例
python3中数组逆序输出方法
python猜数字小游戏实现代码
![](https://img.aspxhome.com/file/2023/5/98805_0s.jpg)
Facebook:产品设计评价体系解密
Python两个整数相除得到浮点数值的方法
Python 类方法和实例方法(@classmethod),静态方法(@staticmethod)原理与用法分析
![](https://img.aspxhome.com/file/2023/7/70237_0s.png)
Vue 实现拨打电话操作
详解mysql8.0创建用户授予权限报错解决方法
python基于openpyxl生成excel文件
![](https://img.aspxhome.com/file/2023/4/82684_0s.png)
python 如何使用requests下载文件
python3.3教程之模拟百度登陆代码分享
python原类、类的创建过程与方法详解
MySQL的一些常用的SQL语句整理
SQL入侵恢复xp_cmdshell方法总结
Httprunner简介、安装及基本使用教程
![](https://img.aspxhome.com/file/2023/0/110260_0s.png)
javascript实现圣旨卷轴展开效果(代码分享)
![](https://img.aspxhome.com/file/2023/1/136801_0s.gif)