Blog / 阅读

PHP 中文大写数字转阿拉伯数字 函数

by admin on 2014-11-10 16:43:21 in ,



函数测试是可用的,不过必须是 中文数字才行。 如果要实际应用,需要实现正则出来中文数字,否则会转换失败。

<?php
echo chtonum('八百二十一');
/*
function: 中文大写数字转阿拉伯数字
*/
function chtonum($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]];
        }
    }
 if(substr($str, -2, 1)=="0")
 {
 $num+=substr($str, -1);
 }
 if($num==0)
 {
 $num=sprintf('%2u',$ChapterName);
 }
 return $num;

}
?>


写评论

相关文章

上一篇:ALLOC_INIT_ZVAL 替代函数 MAKE_STD_ZVAL

下一篇:thinkphp 3.2 写出配置到文件

评论

写评论

* 必填.

分享

栏目

赞助商


热门文章

Tag 云