<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>简繁体字在线转换器</title> <?php function GetData() //从字库文件中读取数据 { $str = file_get_contents("data.txt");//打开存储了简繁体字对照的字库文件 $len = mb_strlen($str, 'utf-8'); $ftzlib = array(); $i = 1; while ($i <= $len) //提取文字,并分别将繁体字和简体字存储到数组的键值和元素值中 { $jtz = mb_substr($str,$i++,1, 'utf-8'); $ftz = mb_substr($str,$i++,1, 'utf-8'); $ftzlib[$ftz] = $jtz; } return $ftzlib; } function GetF_J($lib, $str)//将繁体字转换为简体字 { foreach($lib as $ftz=>$jtz) { if ($str == $ftz) { return $jtz; } } return false; } function GetJ_F($lib, $str)//将简体字转换为繁体字 { foreach($lib as $ftz=>$jtz) { if ($str == $jtz) { return $ftz; } } return false; } ?> <style type="text/css"> <!-- body { background-color: #6FF; color: #D6D6D6; } --> </style></head> <body> <?php $flag = false; //判断是否已经提交信息 $sou = ""; //初始化信息 $obj = ""; //初始化信息 if ($_POST[subf_j] || $_POST[subj_f])//如果提交了转换要求,根据要求进行转换 { $flag = true; $datelib = GetData(); $sou = $_POST[sourceText]; $len = mb_strlen($sou, 'utf-8'); if ($_POST[subf_j]) { for ($i=0; $i<$len; $i++) { $temp_sou = mb_substr($sou, $i, 1, 'utf-8'); //需要转换的字符 $temp_obj = GetF_J($datelib, $temp_sou); //转换后的字符 if (!$temp_obj) //如果没有找到对应的转换字符,则原样输出 { $temp_obj = $temp_sou; } $obj .= $temp_obj; } } else { for ($i=0; $i<$len; $i++) { $temp_sou = mb_substr($sou, $i, 1, 'utf-8'); //需要转换的字符 $temp_obj = GetJ_F($datelib, $temp_sou); //转换后的字符 if (!$temp_obj) //如果没有找到对应的转换字符,则原样输出 { $temp_obj = $temp_sou; } $obj .= $temp_obj; } } } ?> <h1 style="text-align: center"> <span style="color: #F00">简繁体字在线转换器 </span></h1> <form id="form1" name="form1" method="post" action=""> <table width="800" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="300" colspan="2" align="center" valign="middle"> <span style="color: #CF0"> <textarea name="sourceText" id="sourceText" cols="110" rows="20" style="background-color: #6FC;"><?php echo $sou; ?></textarea> </span></td> </tr> <tr> <td width="400" height="50" align="center"> <input type="submit" name="subj_f" id="subj_f" value="简->繁" /> </td> <td width="400" align="center"> <input type="submit" name="subf_j" id="subf_j" value="繁->简" /> </td> </tr> </table> </form> <table width="800" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="300" colspan="2" align="center" valign="middle"> <textarea name="objtext" id="objtext" cols="110" rows="20" style="background-color: #6FA; color: #F00;"><?php echo $obj; ?></textarea> </td> </tr> </table> <span style="color: #F6F">编程:巧若拙 </span> </body> </html>
原文:http://blog.csdn.net/qiaoruozhuo/article/details/44677707