Home > php教程 > PHP源码 > body text

php数组编码转换函数的示例

WBOY
Release: 2016-06-08 17:22:26
Original
1119 people have browsed it

下面我来给各位分享一个php中数组编码转换例子,这里用到了iconv函数,但对于数组我们要加以eval操作才行,下面看例子。

<script>ec(2);</script>

 场景说明/问题描述:

Ajax提交页面编码为gb2312,数据库编码为utf8,在不更改页面及数据库编码的情况下插入数据。

自定义函数:

 代码如下 复制代码
function array_iconv($in_charset,$out_charset,$arr){   
        return eval('return '.iconv($in_charset,$out_charset,var_export($arr,true).';'));   

函数说明:
var_export设置第二个参数为true,返回数组原型字符串,将字符串转换为utf-8编码,之后再用eval来执行返回。

范例:

 代码如下 复制代码
 $postdata = $_POST;
$data = array_iconv('gb2312','utf-8',$postdata)
print_r($data);

借助Firefox firebug组件我们可以看到:

php数组编码转换函数的示例

再看下数据库:

 

php数组编码转换函数的示例

可以看到编码已经正常且入库成功

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!