php函数mult_iconv:转换任意维数组的字符集编码(扩展iconv函数功能)

php的iconv函数只支持对字符串的转换编码,如果是数组,就要自己遍历转换了,因此写如上的函数,对任意维数组进行转换,同时转换下标(索引)及值。

注意:不转对象。

function mult_iconv($in_charset,$out_charset,$data)
{
    if(substr($out_charset,-8)=='//IGNORE'){
        $out_charset=substr($out_charset,0,-8);
    }
    if(is_array($data)){
        foreach($data as $key => $value){
            if(is_array($value)){
                $key=iconv($in_charset,$out_charset.'//IGNORE',$key);
                $rtn[$key]=mult_iconv($in_charset,$out_charset,$value);
            }elseif(is_string($key) || is_string($value)){
                if(is_string($key)){
                    $key=iconv($in_charset,$out_charset.'//IGNORE',$key);
                }
                if(is_string($value)){
                    $value=iconv($in_charset,$out_charset.'//IGNORE',$value);
                }
                $rtn[$key]=$value;
            }else{
                $rtn[$key]=$value;
            }
        }
    }elseif(is_string($data)){
        $rtn=iconv($in_charset,$out_charset.'//IGNORE',$data);
    }else{
        $rtn=$data;
    }
    return $rtn;
}

下载程序文件(含示例)

调用示例:

$foobar=mult_iconv('gbk','utf-8','一个数组、字符串或其它类型数据');

一个复杂一点的测试示例(不转对象)

// ******* 一个复杂一点的测试示例 ********************
class MyClass
{
    public $v1='不转对象,恭喜发财';
    public $v2='skipped object';
    public function f()
    {
        return true;
    }
}
$obj=new MyClass();

$foo=array('abcd','随便写点文字'
    ,array('中文下标'=> 789,'天地玄黄'
        =>array('宇宙洪荒'=>'赵钱孙李',300=>'恭喜发财,不转对象'
                    ,array('更深的数组'=>'照样可以转换')
                )
    )
    ,'恭喜发财,不转对象' => $obj
    ,'如需转对象'=>'Do It Yourself!'
    ,'作者很懒'=>'用不到就不写了'
    );
$bar=mult_iconv('gbk','utf-8//IGNORE',$foo);
var_dump($bar);

php使用iconv进行从utf-8转为gb2312字符编码出错解决方案

[1601004注]这篇文章没啥价值,请读者参考php官方手册吧,官方有中译本

在php函数库有一个函数:iconv()

把gb2312置换成utf-8

$text=iconv("GB2312","UTF-8",$text);

在用$text=iconv("UTF-8","GB2312",$text)过程中,如果遇到一些特别字符时,如:"—",英文名中的"."等等字符,转换就断掉了。这些字符后的文字都没法继续转换了。

针对这的问题,可以用如下代码实现

$text=iconv("UTF-8","GBK",$text);

你没有看错,就这么简单,不使用gb2312,而写成GBK,就可以了。

还有一种方法:

第二个参数,加上//IGNORE,忽略错误,如下:

iconv("UTF-8","GB2312//IGNORE",$data);

没有具体比较这两种方法,感觉第一种(GBK代替gb2312)方法更好.

php手册中iconv() 说明

iconv

(PHP 4 >= 4.0.5, PHP 5)

iconv -- Convert string to requested character encoding

Description

string iconv ( string in_charset, string out_charset, string str )

Performs a character set conversion on the string str from in_charset to out_charset. Returns the converted string or FALSE on failure.

If you append the string //TRANSLIT to out_charset transliteration is activated. This means that when a character can't be represented in the target charset, it can be approximated through one or several similarly looking characters. If you append the string //IGNORE, characters that cannot be represented in the target charset are silently discarded. Otherwise, str is cut from the first illegal character.

例子 1. iconv() example:

<?php
echo iconv("ISO-8859-1", "UTF-8", "This is a test.");
?>

string iconv ( string $in_charset , string $out_charset , string $str )

在使用这个函数进行字符串编码转换时,需要注意,如果将utf-8转换为gb2312时,可能会出现字符串被截断的情况发生。

此时可以使用以下方法解决:

//author:zhxia
$str=iconv('utf-8',"gb2312//TRANSLIT",file_get_contents($filepath));

即在第二个参数出添加红色字部分,表示:如果在目标编码中找不到与源编码相匹配的字符,会选择相似的字符进行转换。

此处也可以使用://IGNORE 这个参数,表示忽略不能转换的字符。