-
//原创内容 ,
-
//主要目的是用于自己记录一些封装好的方法 和 分享以供大家借鉴。 希望大家能提出优化建议。
-
//如若转载 望加来源,谢谢。https://blog.youkuaiyun.com/BetterMan_1/article/details/77646429
//阿拉伯转中文
private function numtochr($num,$mode=true) {
$char = array("零","一","二","三","四","五","六","七","八","九");
$dw = array("","十","百","千","","万","亿","兆");
$dec = "点";
$retval = "";
if($mode)
preg_match_all("/^0*(\d*)\.?(\d*)/",$num, $ar);
else
preg_match_all("/(\d*)\.?(\d*)/",$num, $ar);
if($ar[2][0] != "")
$retval = $dec . $this->ch_num($ar[2][0],false); //如果有小数,先递归处理小数
if($ar[1][0] != "") {
$str = strrev($ar[1][0]);
for($i=0;$i<strlen($str);$i++) {
$out[$i] = $char[$str[$i]];
if($mode) {
$out[$i] .= $str[$i] != "0"? $dw[$i%4] : "";
if($str[$i]+$str[$i-1] == 0)
$out[$i] = "";
if($i%4 == 0)
$out[$i] .= $dw[4+floor($i/4)];
}
}
$retval = join("",array_reverse($out)) . $retval;
}
return $retval;
}
//中文转阿拉伯
private function chrtonum($str){
$num=0;
$bins=array("零","一","二","三","四","五","六","七","八","九",'a'=>"个",'b'=>"十",'c'=>"百",'d'=>"千",'e'=>"万");
$bits=array('a'=>1,'b'=>10,'c'=>100,'d'=>1000,'e'=>10000);
foreach($bins as $key=>$val){
if(strpos(" ".$str,$val)) $str=str_replace($val,$key,$str);
}
foreach(str_split($str,2) as $val){
$temp=str_split($val,1);
if(count($temp)==1) $temp[1]="a";
if(isset($bits[$temp[0]])){
$num=$bits[$temp[0]]+(int)$temp[1];
}else{
$num+=(int)$temp[0]*$bits[$temp[1]];
}
}
return $num;
}
//下面是批量转换 $str是多道题 每道题换行或多余10个空格隔开即可 调用shuziduxie方法即可
private function shuziduxie($str){
$res = array();
$arr = explode(chr(10),$str);
foreach ($arr as $k=>$v){
$v = trim($v);
if(is_numeric($v)){
$question = $v.' 读作:';
$anser = $this->numtochr($v);
}else{
$question = $v.' 写作:';
if(strstr($v,"亿")) {
$v = explode('亿', $v);
$y = $this->chrtonum($v[0]);
$v = $v[1];
}else{
$y = 0;
}
if(strstr($v,"万")){
$v = explode('万',$v);
if(strstr($v[0],"零")){
$n = explode('零',$v[0]);
$a = $this->chrtonum($n[0])+$this->chrtonum($n[1]);
}else{
$a = $this->chrtonum($v[0]);
}
if(strstr($v[1],"零")){
$m = explode('零',$v[1]);
$b = $this->chrtonum($m[0])+$this->chrtonum($m[1]);
}else{
$b = $this->chrtonum($v[1]);;
}
$anser = $y*100000000+$a*10000+$b;
}else{
if(strstr($v,"零")){
$v = explode('零',$v);
$anser = $y*100000000+$this->chrtonum($v[0])+$this->chrtonum($v[1]);
}else{
$anser = $y*100000000+$this->chrtonum($v);;
}
}
}
$res[$k]['question'] = $question;
$res[$k]['question_word'] = $question;
$res[$k]['questionanswer'] = $anser;
$res[$k]['question_answer_word'] = $anser;
}
return $res;
}