PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等

时间:2023-01-06 22:17:24
研究VtigerCRM时,试图把繁体语言插件转成简体版,并去除对iconv的依赖,因为很多php空间不支持;研究过GB2312,Big5,UTF-8之间的转化方法。后来就扔到了一边,最近有多个朋友询问解决方案,于是重新找出来贡献给大家;
本人对php所知不多,即使偶尔修改些php的网页也是摸索着查资料来实现,望php行家莫见笑。
这个方案的特点就是使用简单,只要在需要调用转换函数的页面加入include_once'language.inc',就可以方便使用函数了。
关于使用方法就附上我当初转语言插件的代码:
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等<? php
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等    
function  dirtree( $path = " . "
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等    {
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等        
include_once   ' language.inc ' ;
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等        
$d   =   dir ( $path ); 
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等        
while ( false   !==  ( $v   =   $d -> read()))
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等        { 
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等            
if ( $v   ==   " . "   ||   $v   ==   " .. "
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等                
continue
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等            
$file   =   $d -> path . " / " . $v
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等            
if ( is_dir ( $file )) 
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等            { 
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等                
// echo "$v"; 
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等
                dirtree( $file ); 
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等            } 
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等            
else  
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等                
echo   $file . " \n " ;
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等                
// rename($file,str_replace('zh_tw','zh_cn',$file));
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等                //$filename='zh_cn.lang.php';                

PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等
                 $fd = fopen ( $file , ' r ' );                
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等                
$contents   =   fread ( $fd ,   filesize ( $file ));                
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等                
$contents = u2b( $contents );
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等                
$contents = b2g( $contents );
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等                
// echo $contents;
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等
                 $contents = str_replace ( ' 日 ' , ' 日历 ' , $contents );
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等                
$contents = g2u( $contents );
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等                
$contents = str_replace ( ' Trade chinese language pack created by METO, Inc. http://www.meto.com.tw ' , ' Simple chinese language pack created by Steed Soft Technology, Inc. http://www.steedsoft.com ' , $contents );
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等                
fclose ( $fd );
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等                
$fd = fopen ( str_replace ( ' zh_tw/ ' , ' zh_cn/ ' , str_replace ( ' zh_tw ' , ' zh_cn ' , $file )) , ' w ' );
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等                
fwrite ( $fd , $contents );
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等                
fclose ( $fd );
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等                
// rename($file,str_replace('zh_tw','zh_cn',$file));                
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等
        }
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等         
$d -> close(); 
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等         
// echo " "; 
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等
     } 
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等     dirtree(
" zh_tw " ); 
PHP版本繁简体,UTF-8和Unicode互转的函数:GB2312Big5,GB2312UTF-8,Big5UTF-8等 
?>

 

下载地址
也可以登陆该网页下载:http://www.layman.5gigs.com/download/(还要些别的好东西等着你)

P.S
该方案的缺点,因为繁体中文和简体中文存在一定的词法不一致的情况,因此会发现有些词无法转换,我从网上找到了两个处理该问题的dat文件,但具体实现我没有做,以后也许会抽时间研究下并做个更新。