工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 1381|回复: 0

[php]在*nix正确的转码

[复制链接]
发表于 2006-10-30 03:32 | 显示全部楼层 |阅读模式
在php中把gbk(gb2312)或者big5码转成utf-8码是比较常用的操作。有两个方法可以实现下面的转换。
优先使用mb_convert_encoding,转换的效果比较好,其次是iconv,加上//TRANSLIT保证在原编码数据出错时能够忽略错误。
以下两个函数需要mb_string 或者iconv库支持,如果都不被支持,只能使用php加载编码转换表来支持编码转换,效率比较低下。


  1.         function CharsetFromTo( $sText, $sCharsetFrom, $sCharsetTo ) {               
  2.                 if ( function_exists( 'mb_convert_encoding' ) ) {
  3.                         return mb_convert_encoding( $sText, $sCharsetTo, $sCharsetFrom );
  4.                 }
  5.                 else if ( function_exists( 'iconv' ) ){
  6.                         return iconv( $sCharsetFrom, $sCharsetTo . '//TRANSLIT', $sText ) ;
  7.                 }
  8.                 else {
  9.                         //to throw an exception or die
  10.                 }
  11.         }
复制代码

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2025-5-15 07:16

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表