PHPÖÐiconvº¯Êý֪ʶ»ã×Ü

5年以前  |  阅读数:900 次  |  编程语言:PHP 

½ñÌiÔÚÐÞ¸ÄÂÛÎÄÔÚÏßµÄʱºo£¬Óoµ½ÁËiconvÕa¸oº¯Êý¡£Ñ§Ï°Ò»ÏÂ


    header('Content-Type: application/vnd.ms-excel;charset=UTF-8"');
    $name=iconv('utf-8', 'gb2312', $data['year'].'Äe£¬µÚ'.$data['period'].'ÆÚͨÐż');
    header('Content-Disposition: attachment;filename="' . $name . '.xls"');
    header('Cache-Control: max-age=0');

Õa¶Î´uÂeµÄÒa˼£¬¾ÍÊÇ°Ñutf-8¸ñʽת»¯Îªgb2312µÄ¸ñʽ£¬È»ºo¸³Öµ¸ø$name,ÕaÑuµ¼³oexcelÎļþµÄÃu×ÖµÄʱºo£¬¾ÍÊÇÖÐÎĵÄ$nameµÄÃu×Ö¡£
ϱßÊǹØÓÚÕa¸oº¯ÊýµÄÏeϸºÍÑÓÉiÓÃ*¨


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

ignoreµÄÒa˼ÊǺoÂÔת»»Ê±µÄ´iÎo£¬Èç¹uûÓÐignore²ÎÊý£¬ËuÓиÃ×ÖuºoÃaeµÄ×Öu´®¶¼ÎÞ*¨±»±£´ae¡£

Õa¸oiconv()Õa¸oº¯Êý,ÔÚphp5ÖÐÊÇÄÚÖõÄ.лл.

Àý×Ó£º


     <?php
     echo $str= 'ÄaºÃ,ÕaÀiÊÇÂo¿§*È!';
     echo '<br />';
     echo iconv('GB2312', 'UTF-8', $str);   //½«×Ö*u´®µÄ±aÂe´ÓGB2312תµ½UTF-8
     echo '<br />';

     echo iconv_substr($str, 1, 1, 'UTF-8');  //°´×Ö*u¸oÊý½ØÈ¡¶ø*Ç×Ö½Ú     
     print_r(iconv_get_encoding());      //µÃµ½µ±Ç°Ò³Ãae±aÂeÐÅÏ¢

     echo iconv_strlen($str, 'UTF-8');     //µÃµ½Ée¶¨±aÂeµÄ×Ö*u´®³¤¶È

    //Ò²ÓÐÕaÑuÓõÄ

     $content = iconv("UTF-8","gbk//TRANSLIT",$content); 

    ?>

iconv²»ÊÇphpµÄĬÈϺ¯Êý£¬Ò²ÊÇĬÈÏ°²×°µÄÄ£¿e¡£ÐeÒª°²×°²ÅÄÜÓõġ£

Èç¹uÊÇwindows2000+php£¬Äa¿ÉÒÔÐÞ¸Äphp.iniÎļþ£¬½«extension=php_iconv.dllÇ°µÄ";"È¥µo£¬Í¬Ê±ÄaÒªcopyÄaµÄÔ­php°²×°ÎļþϵÄiconv.dllµ½ÄaµÄwinnt/system32Ï£¨Èç¹uÄaµÄdllÖ¸ÏoµÄÊÇÕa¸oĿ¼£(C)

ÔÚlinux»¾³Ï£¬Óþ²Ì¬°²×°µÄ½Ê½£¬ÔÚconfigureʱ¼Ó¶aÒ»Ïi --with-iconv¾Í¿ÉÒÔÁË£¬phpinfo¿´µÃµ½iconvµÄÏi¡£(Linux7.3+Apache4.06+php4.3.2)£¬

ÏÂÔØ£ºftp://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.8.tar.gz
°²×°£º


    #cp libiconv-1.8.tar.gz /usr/local/src
    #tar zxvf lib*
    #./configure --prefix=/usr/local/libiconv
    #make
    #make install

±aÒephp

./configure --prefix=/usr/local/php4.3.2 --with-iconv=/usr/local/libiconv/

ʹÓõļoµ¥Àý×Ó£º


    <?php 
     echo iconv("gb2312","ISO-8859-1","ÎÒÃÇ"); 
    ?>

PHPÖеÄmb_convert_encodingÓeiconvº¯Êý½eÉÜ

mb_convert_encodingÕa¸oº¯ÊýÊÇÓÃÀ´×ª»»±aÂeµÄ¡£Ô­À´Ò»Ö±¶Ô³ÌÐo±aÂeÕaÒ»¸ÅÄi²»Ài½a£¬²»¹ýÏÖÔÚºÃÏñÓеa¿ªÇÏÁË¡£
²»¹ýÓ¢ÎÄÒ»°a²»»a´aeÔÚ±aÂeÎÊÌa£¬Ö»ÓÐÖÐÎÄÊý¾Ý²Å»aÓÐÕa¸oÎÊÌa¡£±ÈÈçÄaÓÃZend Studio»oEditplusд³ÌÐoʱ£¬ÓõÄÊÇgbk±aÂe£¬Èç¹uÊý¾ÝÐeÒªÈeÊý¾Ý¿a£¬¶øÊý¾Ý¿aµÄ±aÂeΪutf8ʱ£¬Õaʱ¾ÍÒª°ÑÊý¾Ý½øÐбaÂeת»»£¬²»È»½øµ½Êý¾Ý¿a¾Í»a±a³ÉÂÒÂe¡£

mb_convert_encodingµÄÓè¼u¹Ù½£º
http://cn.php.net/manual/zh/function.mb-convert-encoding.php

×oÒ»¸oGBK To UTF-8


    < ?php 
     header("content-Type: text/html; charset=Utf-8"); 
     echo mb_convert_encoding("Š…‚SÎÒµÄÓÑ×Ð", "UTF-8", "GBK"); 
    ?> 

ÔÙÀ´¸oGB2312 To Big5


    < ?php 
     header("content-Type: text/html; charset=big5"); 
     echo mb_convert_encoding("ÄaÊÇÎÒµÄÅoÓÑ", "big5", "GB2312"); 
    ?> 

²»¹ýҪʹÓÃÉÏÃaeµÄº¯ÊýÐeÒª°²×°µ«ÊÇÐeÒªÏÈenable mbstring À(C)Õ¹¿a¡£

PHPÖеÄÁiÍaÒ»¸oº¯ÊýiconvÒ²ÊÇÓÃÀ´×ª»»×Ö*u´®±aÂeµÄ£¬ÓeÉϺ¯Êý¹¦ÄÜÏaËÆ¡£

ÏÂÃae»¹ÓÐÒ»Ð(C)ÏeϸµÄÀý×Ó£º


    iconv ¡ª Convert string to requested character encoding
    (PHP 4 >= 4.0.5, PHP 5)
    mb_convert_encoding ¡ª Convert character encoding
    (PHP 4 >= 4.0.6, PHP 5)

ÓÃ*¨£º

string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )
ÐeÒªÏÈenable mbstring À(C)Õ¹¿a£¬ÔÚ php.iniÀi½«; extension=php_mbstring.dll Ç°ÃaeµÄ ; È¥µo
mb_convert_encoding ¿ÉÒÔÖ¸¶¨¶aÖÖÊaÈe±aÂe£¬Ëu»a¸u¾ÝÄÚÈÝ×Ô¶¯Ê¶±ð,µ«ÊÇÖ´ÐÐЧÂʱÈiconv²iÌ«¶a£»

string iconv ( string in_charset, string out_charset, string str )
×¢Òa£ºµÚ¶þ¸o²ÎÊý£¬³ýÁË¿ÉÒÔÖ¸¶¨Òª×ª»¯µ½µÄ±aÂeÒÔÍa£¬»¹¿ÉÒÔÔo¼ÓÁ½¸oºo׺£º//TRANSLIT ºÍ //IGNORE£¬ÆaÖÐ //TRANSLIT »a×Ô¶¯½«²»ÄÜÖ±½Óת»¯µÄ×Öu±a³ÉÒ»¸o»o¶a¸o½uËƵÄ×Öu£¬//IGNORE »aºoÂÔµo²»ÄÜת»¯µÄ×Öu£¬¶øĬÈÏЧ¹uÊÇ´ÓµÚÒ»¸oǨ×Öu½Ø¶Ï¡£
Returns the converted string or FALSE on failure.

ʹÓãº

¢ÏÖiconvÔÚת»»×Öu¡±¡ª¡±µ½gb2312ʱ»a³o´i£¬Èç¹uûÓÐignore²ÎÊý£¬ËuÓиÃ×ÖuºoÃaeµÄ×Öu´®¶¼ÎÞ¨±»±£´ae¡£²»¹ÜÔoôÑu£¬Õa¸o¡±¡ª¡±¶¼Îިת»»³É¹¦£¬ÎÞ*¨Êa³o¡£ ÁiÍamb_convert_encodingûÓÐÕa¸obug.

Ò»°aÇe¿oÏÂÓà iconv£¬Ö»Óе±Óoµ½Îިȶ¨Ô­±aÂeÊǺÎÖÖ±aÂe£¬»oÕßiconvת»¯ºoÎÞ*¨Õý³£ÏÔʾʱ²ÅÓÃmb_convert_encoding º¯Êý.

from_encoding is specified by character code name before conversion. it can be array or string - comma separated enumerated list. If it is not specified, the internal encoding will be used.
/ Auto detect encoding from JIS, eucjp-win, sjis-win, then convert str to UCS-2LE /
$str = mb_convert_encoding($str, ¡°UCS-2LE¡±, ¡°JIS, eucjp-win, sjis-win¡±);
/ ¡°auto¡± is expanded to ¡°ASCII,JIS,UTF-8,EUC-JP,SJIS¡± /
$str = mb_convert_encoding($str, ¡°EUC-JP¡±, ¡°auto¡±);

Àý×Ó£º


     $content = iconv(¡±GBK¡±, ¡°UTF-8¡a, $content); 
     $content = mb_convert_encoding($content, "UTF-8¡a,"GBK"); 

phpÖÐʹÓÃiconvº¯ÊýʱÈÝÒ׺oÂԵIJÎÊý

½ñÌiÔÚ´¦ÀiץȡÄÚÈݵÄʱºo£¬µ±²ÉÓÃiconv½øÐбaÂeת»»µÄʱºo£¬¢ÏÖ½a¹u»aÖжϣ¬²ÂÊÇ×Öu¼¯µÄÎÊÌa£¬¿¼ÂÇÔoôÌø¹ýÄ¿±e×Öu¼¯²»´aeÔÚµÄ×Öu£¬²eÊÖ²a¢ÏÖiconvµÄº¯ÊýÖ»ÓÐÈý¸o²ÎÊý£¬ºÃÏñ²»ÐУ¬È»ºo²eÍøÉÏÓÐÈË˵¿ÉÒÔ£¬µ«ÊǺÜÆae¹ÖÔoôʵÏÖ£¬×iºo¢ÏÖÓ¢ÎÄÃeÊoÓÐ˵¿ÉÒÔ¼Ó±eʶµ½Ä¿±e±aÂeºoÃae£º¡°TRANSLIT¡±£¬ºÜÓoÃÆÔoô¼ÓÄØ£¿Ô­À´ÊÇÏȼӡ°//¡±£¬ÕaeÊÇÓoÃÆ£¬¾¹È»ÓÐÕaÑuµÄÉe¼Æ
Ô­ÐÍ£º $txtContent = iconv("utf-8",'GBK',$txtContent);

ÌØÊa²ÎÊý£ºiconv("UTF-8","GB2312//IGNORE",$data)

Á½¸o¿ÉÑ¡µÄ¸¨Öu²ÎÊý£ºTRANSLITºÍIGNORE £¬(ÆaÖÐIGNORE ¾ÍÊÇ˵Óoµ½ÎÞ*¨×ª»»µÄ¾ÍÌø¹ý)¡£

ÒÔÉÏËuÊo¾ÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝÁË£¬Ï£Íu´o¼ÒÄܹ»Ï²»¶¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例