码迷,mamicode.com
首页 > 其他好文 > 详细

简繁体字在线转换器(改进版)

时间:2015-03-30 09:15:38      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

<!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;
}
?>

<style type="text/css">
<!--
body {
	background-color: #6FF;
	color: #D6D6D6;
}
-->
</style></head>

<body>
<?php	
$sou = "";     //初始化信息
$obj = "";     //初始化信息


if ($_POST[subf_j] || $_POST[subj_f])//如果提交了转换要求,根据要求进行转换
{
	$libf_j = GetData();  //键是繁体字,值是简体字的数组
	ksort($libf_j);  
    array_shift($libf_j);  //注意:去掉空元素,才能执行函数strtr() 
    $libj_f = array_flip($libf_j);  //键是简体字,值是繁体字的数组
	$sou = $_POST[sourceText];
	
	/*
	定义和用法
	strtr() 函数转换字符串中特定的字符。
	语法
	strtr(string,from,to)
	或者
	strtr(string,array)
	参数	描述
	string1	必需。规定要转换的字符串。
	from	必需(除非使用数组)。规定要改变的字符。
	to	必需(除非使用数组)。规定要改变为的字符。
	array	必需(除非使用 from 和 to)。一个数组,其中的键是原始字符,值是目标字符。
	*/

	if ($_POST[subf_j])
	{
		$obj = strtr($sou, $libf_j);
	}
	else
	{ 
		$obj = strtr($sou, $libj_f); 
	}  
}
?>

<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/44745711

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!