UTF8编码内的繁简转换的PHP类

2015-01-24信息快讯网

在网上找了很久都没有找到UTF8字符集内的繁简转换,或许网上已经有人写过这样的代码。

曾找过一个JS版的,但是到了UTF8编码里不能用,于是就产生了自已写一个的念头。其实我这个代码的实现原理很简单的,只是一个替换字符集的过程,相信大部份人都可以写得出来。以下是代码,不知道有没有bug,大家如果在使用上有问题,可以提出来
 
<?php 
//<meta charset=utf-8"> 
class utf8_chinese 
{ 
private $utf8_gb2312; 
private $utf8_big5; 
public function __construct(){ 
$this->utf8_gb2312 = "万与丑专业丛东丝丢两严丧个丬丰临为丽举么义乌乐乔习乡书买乱争于亏云亘亚产亩亲亵亿仅从仑仓仪们价众优伙会伛伞伟传伤伥伦伧伪伫体余佣佥侠侣侥侦侧侨侩侪侬俣俦俨俩俪俭债倾偬偻偾偿傥傧储傩儿兑兖党兰关兴兹养兽冁内冈册写军农冢冯冲决况冻净凄凉凌减凑凛几凤凫凭凯击凼凿刍划刘则刚创删别i刭刽刿剀剂剐剑剥剧劝办务劢动励劲劳势勋勐衷蓉胸亚医华协单卖卢卤卧卫却卺厂厅历厉压厌厍厕厢厣厦厨厩厮县参λ发变叙叠叶号叹叽吁后吓吕吗吣吨听启吴呒呓呕呖呗员呙呛呜咏咔咙咛咝咤咴咸哌响哑哒哓哔哕哗哙哜哝哟唛y唠|唢唣唤唿啧啬啭啮啸喷喽喾嗫呵嗳嘘嘤嘱噜噼嚣嚯团园囱围囵国图圆圣圹场坂坏块坚坛坜坝坞坟坠垄垅垆垒垦垧垩垫垭垲垴埘埙埚埝埯堑堕G墙壮声壳壶状Ρ父垂煌房浼卸徂欺挤芙卑伦备韭桢妪妫姗姜娄娅娆娇娈娱娲娴O婴婵婶媪嫒嫔嫱嬷孙学孪宁宝实宠审宪宫宽宾寝对寻导寿将尔尘尧尴尸尽层撂虢焓袈佩鹩焖昶襻岗岘岙岚岛岭岳岽岿N峄峡i峤峥峦崂崃崭嵘箩吾嗅蔗酃巯币帅师帏帐帘帜带帧帮帱帻帼幂幞干并广庄庆庐庑库应庙庞废鲡蘅异弃张弥弪弯弹强归当录ρ宄咕夺庥忆忏忧忾怀态怂怃怄怅怆怜总怼怿恋恳恶恸恹恺恻恼恽悦悫悬悭悯惊惧惨惩惫惬惭惮惯愍愠愤愦愿慑\憷懑懒懔戆戋戏戗战戬户扎扑扦执扩扪扫扬扰抚抛抟抠抡抢护报担拟拢拣拥拦拧拨择挂挚挛ノ翁⑿挠挡挢挣挤挥捞损捡换捣据捻掳掴掷掸掺掼揸揽揿搀搁搂搅携摄摅摆摇摈摊撄撑撵撷撸撺擞攒敌敛数斋斓斗斩断无旧时旷D昙昼o显晋晒晓晔晕晖暂暧札术朴机杀杂权条来杨杩杰极构枞枢枣枥枧枨枪枫枭柜柠柽栀栅标栈栉栊栋栌栎栏树栖样栾桊桠桡桢档桤桥桦桧桨桩梦检棂椁椟椠椤椭楼榄榇榈榉槛槟槠横樯樱橥橱橹橼檐檩欢欤欧歼殁殇残殒殓殚殡殴毁毂毕毙毡毵氇气氢氩氲汇汉污汤汹沓沟没沣沤沥沦沧h沩沪m泞泪泶泷泸泺泻泼泽泾洁洒洼浃浅浆浇浈缸遣怃济浏夯脘芭ㄤ煌坑刻卫凿盗颁段榛恋尤蠼д巧淀渊渌渍渎渐渑渔渖渗温游湾湿溃溅溆句涔鲋弯黉苈滢滤滥滦滨滩漤潆潇潋潍潜潴澜濑濒灏灭灯灵灾灿炀炉炖炜炝点炼炽烁烂烃烛烟烦烧烨烩烫烬热焕焖焘煅煳熘爱爷牍牦牵牺犊犟状犷犸犹狈狍A狞独狭狮狯狰狱狲猃猎猕猡猪猫猬献獭玑_`玛玮环现o玺珉珏珐珑珲Q琏琐琼瑶瑷璇璎瓒瓮瓯电画畅畲畴疖疗疟疠疡疬疮疯疱疴痈痉痒痖痨痪痫痴瘅}瘗瘘瘪瘫瘾瘿癞癣癫癯皑皱皲盏盐监盖盗盘眍眦着睁睐睑瞒瞩矫矶矾矿砀码砖砗砚砜砺砻砾础n硅硕硖硗}~确硷碍碛碜碱碹磙礼t祢祯祷祸禀禄禅离秃秆种积称秽稆税稣稳穑穷窃窍窑窜窝窥窦窭竖竞笃笋笔笕笺笼笾筑筚筛Y筝筹签简箦箧箨箩箪箫篑篓篮篱簖籁籴类籼粜粝粤粪粮糁糇紧絷纟纠纡红纣纤纥约级纨纩纪纫纬纭纯纰纱纲纳纵纶纷纸纹纺纽纾线绀绁绂练组绅细织终绉绊绋绌绍绎经绐绑绒结绔绕绗绘给绚绛络绝绞统绠绡绢绣绥绦继绨绩绪绫续绮绯绰绱绲绳维绵绶绷绸绺绻综绽绾绿缀缁缂缃缄缅缆缇缈缉缋缌缍缎缏缑缒缓缔缕编缗缘缙缚缛缜缝缟缠缡缢缣缤缥缦缧缨缩缪缫缬缭缮缯缰缱缲缳缴缵罂网罗罚罢罴羁羟羡翘耢耧耸耻聂聋职聍联聩聪肃肠肤肷肾肿胀胁胆胜胧胨胪胫胶脉脍脏脐脑脓脔脚脱脶脸腊腌N腭腻腼腽腾膑H舆舣舰舱舻艰艳艹艺节芈芗芜芦苁苇苈苋苌苍苎苏苘苹茎茏茑茔茕茧荆荐Q荚荛荜荞荟荠荡荣荤荥荦荧荨荩荪荫荬荭荮药莅莜莱莲莳莴莶获莸莹莺莼[萝萤营萦萧萨葱蒇蒉蒋蒌蓝蓟蓠蓣蓥蓦蔷蔹蔺蔼蕲蕴薮藁藓虏虑虚虫虬虮虽虾虿蚀蚁蚂蚕蚝蚬蛊蛎蛏蛮蛰蛱蛲蛳蛴蜕蜗蜡蝇蝈蝉蝎蝼蝾螨]衅衔补衬衮袄袅袜袭B装裆T裢裣裤裥褛褴襁[见观_规觅视觇览觉觊觋觌`觎觏觐觑觞触觯誉誊讠计订讣认讥讦讧讨让讪讫训议讯记讲讳讴讵讶讷许讹论讼讽设访诀证诂诃评诅识诈诉诊诋诌词诎诏译诒诓诔试诖诗诘诙诚诛诜话诞诟诠诡询诣诤该详诧诨诩诫诬语诮误诰诱诲诳说诵诶请诸诹诺读诼诽课诿谀谁谂调谄谅谆谇谈谊谋谌谍谎谏谐谑谒谓谔谕谖谗谘谙谚谛谜谝谟谠谡谢谣谤谥谦谧谨谩谪谫谬谭谮谯谰谱谲谳谴谵谶谷k贝贞负O贡财责贤败账货质贩贪贫贬购贮贯贰贱贲贳贴贵贶贷贸费贺贻贼贽贾贿赀赁赂赃资赅赆赇赈赉赊赋赌赍赎赏赐PQ赓赔赕赖R赘赙赚赛赜赝赞S赠赡赢赣W赵赶趋趱趸跃跄跖跞践Q跷跸跹跻踊踌踪踬踯蹑蹒蹰蹿躏躜躯车轧轨轩a轫转轭轮软轰轱轲轳轴轵轶轷轸轹轺轻轼载轾轿b辁辂较辄辅辆辇辈辉辊辋c辍辎辏辐辑d输辔辕辖辗辘辙辚辞辩辫边辽达迁过迈运还这进远违连迟迩迳迹适选逊递逦逻遗遥邓邝邬邮邹邺邻郁郄郏郐郑郓郦郧郸酝N酱酽酾酿释里鉴銮錾钆钇针钉钊钋钌钍钎钏钐钒钓钔钕钗钙钚钛钝钞钟钠钡钢钣钤钥钦钧钨钩钪钫钬钭钮钯钰钱钲钳钴钵钶钷钸钹钺钻钼钽钾钿铀铁铂铃铄铅铆铈铉铊铋铍铎铐铑铒铕铗铘铙铛铜铝铞铟铠铡铢铣铤铥铧铨铪铫铬铭铮铯铰铱铲铳铴铵银铷铸铹铺铼铽链铿销锁锂锃锄锅锆锇锈锉锊锋锌锍锎锏锐锑锒锓锔锕锖锗错锚锞锟锡锢锣锤锥锦锨锩锫锬锭键锯锰锱锲A锴锵锶锷锸锹锺锻锼B锾锿镀镁镂C镆镇D镉镊镌镍镎镏镐镑镒F镖镗镙G镛镜镝镞镟H镡镢镣镤镥镦镧镨镩镪镫镬镭I镯镰镱镲镳J镶长门闩闪闫\闭问闯闰闱闲闳间闵闶闷闸闹闺闻闼闽闾]阀阁阂阃阄阅阆^阈阉阊阋阌阍阎阏阐阑阒_阔阕阖阗`阙阚a队阳阴阵阶际陆陇陈陉陕陧陨险随隐隶隽难雏雠雳雾霁霉霭靓静靥鞑鞒鞯鞴韦韧韩韪韫韬韵页顶顷顸项顺须顼顽顾顿颀颁颂颃预颅领颇颈颉颊F颌颍G颏颐频H颓颔I颖颗题J颚颛颜额颞颟颠颡颢K颤颥颦颧风rs飑飒飓t飕uv飘飙飚飞飨餍饥饧饨饩饪饫饬饭饮饯饰饱饲饴饵饶饷饺饼饽饿馀馁馄馅馆馇馈馊馋@馍A馏馐馑馒馓馔馕马驭驮驯驰驱R驳驴驵驶驷驸驹驺驻驼驽驾驿骀骁骂S骄骅骆骇骈T骊骋验UV骏骐骑骒骓WX骖骗骘Y骚骛骜骝骞骟骠骡骢骣骤骥Z骧髅髋髌鬓魇魉鱼鱿鲁鲂鲅鲆鲇鲈鲋鲍鲎鲐鲑鲒鲔鲕鲚鲛鲜鲞鲟鲠鲡鲢鲣鲤鲥鲦鲧鲨鲩鲫鲭鲮鲰鲱鲲鲳鲴鲵鲶鲷鲸鲺鲻鲼鲽鳃鳄鳅鳆鳇鳊鳋鳌鳍鳎鳏鳐鳓鳔鳕鳖鳗鳘鳙鳜鳝鳞鳟鳢@鸟鸠鸡鸢鸣\鸥鸦]鸨鸩鸪鸫鸬鸭^鸯_鸱鸲鸳`鸵鸶鸷鸸鸹鸺ab鸽鸾鸿c鹁鹂鹃鹄鹅鹆鹇鹈鹉鹊鹋鹌d鹎鹏e鹑fgh鹕i鹗鹘鹚鹛鹜k鹞lmno鹣鹤p鹦鹧鹨鹩鹪鹫鹬鹭r鹰鹱s鹳t鹾麦麸黄黉d黩黪黾鼋鼍鼗鼹齄齐齑齿龀龃龄龅龆龇龈龉龊龋龌龙龚龛龟志制咨只里系范松没尝尝闹面准钟别闲干尽脏拼"; 
$this->utf8_big5 = "fchI|zG爿SR辂eNx塘lIy於aHC|H}xr夥t畜wNLbHeS~zRzAEf键hmPdBFrVTnQrQDRpC坐PD{P腓cthe}q褶k烀QT^tAf钨uRuPlsSdBNPh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tnky^gSMaORCBhu|jZDGCOdsnLTVWZ\]Jcebgh`l[|Y}GywubA]U@THDFIRXAH]EUS[`hyrZFVq\nov^XdxfghntywBDC@ABIHiRacM}忸WUl_jhefw}~DhAEL^QRSZ\`_dhjjw}hqTDAEFGLINHKRQW^lktvxos}~zRSWZY_gHzx|vw{A~RQPGHEUTSKR_sj}\tq~EKLJtyxW|u~EGT|OWVNUcQTq^wnbjf`dqor~\~zaNOEHKAFTLYXalsl[gw{qvmeFcBLMI@ZX[VshkgBFuSQOtfIdcR{o|zxrv@[MPZN]ZOY^ogAliktX\BFg_OVW^YQsWpwDILXUzSZstowx{BORWXZ[]eg_fblrpx}Iu谘beSL\I淑f乾K拚"; 
} 
public function gb2312_big5($str) { 
$str_t = ''; 
$len = strlen($str); 
$a = 0; 
while ($a < $len){ 
if (ord($str{$a})>=224 && ord($str{$a})<=239){ 
if (($temp = strpos( $this->utf8_gb2312, $str{$a} . $str{$a+1} . $str{$a+2})) !== false){ 
$str_t .= $this->utf8_big5{$temp} . $this->utf8_big5{$temp+1} . $this->utf8_big5{$temp+2}; 
$a += 3; 
continue; 
} 
} 
$str_t .= $str{$a}; 
$a += 1; 
} 
return $str_t; 
} 
public function big5_gb2312($str) { 
$str_t = ''; 
$len = strlen($str); 
$a = 0; 
while ($a < $len){ 
if (ord($str{$a})>=224 && ord($str{$a})<=239){ 
if (($temp = strpos( $this->utf8_big5, $str{$a} . $str{$a+1} . $str{$a+2})) !== false){ 
$str_t .= $this->utf8_gb2312{$temp} . $this->utf8_gb2312{$temp+1} . $this->utf8_gb2312{$temp+2}; 
$a += 3; 
continue; 
} 
} 
$str_t .= $str{$a}; 
$a += 1; 
} 
return $str_t; 
} 
} 
//本文档运行于UTF8编码 PHP100.com 
header("Content-Type: text/html; charset=utf-8"); 
$chinese = new utf8_chinese; 
//UTF8内简转繁 
$str = "中华人民共和国万岁"; 
$str_big5 = $chinese->gb2312_big5($str); 
echo "原文:$str <br />"; 
echo "转换为繁体后: $str_big5 <br />"; 
//UTF8内繁转简 
$str = "中A人民共和fq "; 
$str_gb2312 = $chinese->big5_gb2312($str); 
echo "原文: $str <br /> "; 
echo "转换为简体后:$str_gb2312 <br /> " ; 
?> 
©2014-2024 dbsqp.com