获取中文字符串中的数字并转换成阿拉伯数字
来源:互联网 发布:太空工程师可视化编程 编辑:程序博客网 时间:2024/05/22 15:08
获取中文字符串中的数字并转换成阿拉伯数字
<?phpfunction charN2intN($str, $solve_discrete = true){ //个位数字 $arr_char_unit = array('1一壹','2二贰两','3三叁','4四肆','5五伍','6六陆','7七柒','8八捌','9九玖'); //计数单位 $arr_char_digit = array('0零','十拾','百佰','千仟','万','亿'); //提取数字 $temp_arr_match_num = "1一壹2二贰两3三叁4四肆5五伍6六陆7七柒8八捌9九玖0零十拾百佰千仟万亿"; $temp_len = iconv_strlen($str,'utf-8'); $temp_ans = ""; $temp_start = false; for($i = 0;$i < $temp_len;$i++) { $temp_char = mb_substr($str,$i,1,'utf-8'); if((stripos($temp_arr_match_num, $temp_char) > -1)) { $temp_start = true; $temp_ans .= $temp_char; } else if(!$solve_discrete && $temp_start) { //$solve_sp:false,只处理连续的数 break; } } $str = $temp_ans; $str_len = iconv_strlen($str,'utf-8'); //结构编码 $objs_code = ""; //结构 $objs = []; for($i = 0;$i < $str_len;$i++) { //当前字符 $temp_char = mb_substr($str,$i,1,'utf-8'); //是否数字 $isNum = false; for($index = 0 ; $index < count($arr_char_unit);$index++) { if((stripos($arr_char_unit[$index], $temp_char) > -1)) { $objs_code .='u'; $objs[] = [ 'type' => 'unit', 'value' => $index+1 ]; $isNum = true; break; } } if($isNum){continue;} //是否计数单位 for($index = 0 ; $index < count($arr_char_digit);$index++) { if((stripos($arr_char_digit[$index], $temp_char) > -1)) { if(0 != $index) { $objs_code .='d'; } $objs[] = [ 'type' => 'digit', 'value' => (0 == $index?0:pow(10,$index)) ]; break; } } } $ans = ""; //根据结构编码选择组合方式 if(preg_match("/^u+?$/u", $objs_code, $ms)) { foreach ($objs as $obj) { $ans .= $obj['value']; } } else if(preg_match("/^[ud]{0,1}(ud|du)*[ud]*$/u", $objs_code, $ms)) { $unit = -1; $temp_ans = 0; foreach ($objs as $obj) { if('unit' == $obj['type']) { if(-1 < $unit) { $temp_ans += $unit; $ans .= $temp_ans; $temp_ans = 0; } $unit = $obj['value']; } else if('digit' == $obj['type']) { if(0 < $obj['value']) { if(-1 < $unit) { $temp_ans += $unit*$obj['value']; } else { $temp_ans += $obj['value']; } } $unit = -1; } } if(-1 < $unit) { $temp_ans += $unit; $ans .= $temp_ans; } if("" == $ans) { $ans .= $temp_ans; } } return $ans;}// 测试数据echo "通过的正常格式\n";$temp = '从七开始';echo charN2intN($temp)."\t\t\t".$temp;echo "\n";$temp = '从十开始';echo charN2intN($temp)."\t\t\t".$temp;echo "\n";$temp = '从十七开始';echo charN2intN($temp)."\t\t\t".$temp;echo "\n";$temp = '从七十开始';echo charN2intN($temp)."\t\t\t".$temp;echo "\n";$temp = '从七十七开始';echo charN2intN($temp)."\t\t\t".$temp;echo "\n";$temp = '从七佰零七开始';echo charN2intN($temp)."\t\t\t".$temp;echo "\n";$temp = '从七佰一十七开始';echo charN2intN($temp)."\t\t\t".$temp;echo "\n";$temp = '从七佰七十七开始';echo charN2intN($temp)."\t\t\t".$temp;echo "\n";$temp = '从七千零七开始';echo charN2intN($temp)."\t\t".$temp;echo "\n";$temp = '从七千零七十七开始';echo charN2intN($temp)."\t\t".$temp;echo "\n";$temp = '从七千七佰零七开始';echo charN2intN($temp)."\t\t".$temp;echo "\n";$temp = '从七千七佰七十七开始';echo charN2intN($temp)."\t\t".$temp;echo "\n";echo "\n";echo "通过的非正常格式\n";$temp = '从七千零七十七开始到12';echo charN2intN($temp)."\t\t".$temp;echo "\n";$temp = '从一千四百八十七开始到12';echo charN2intN($temp)."\t\t".$temp;echo "\n";$temp = '从五七开始到七零七零零';echo charN2intN($temp)."\t\t".$temp;echo "\n";$temp = '从五七开始到七零七00';echo charN2intN($temp)."\t\t".$temp;echo "\n";$temp = '从203开始到七零七';echo charN2intN($temp)."\t\t".$temp;echo "\n";$temp = '从203开始到707';echo charN2intN($temp)."\t\t".$temp;echo "\n";$temp = '从一千四百八十开始到12';echo charN2intN($temp)."\t\t".$temp;echo "\n";$temp = '从七千零七十七开始到七千零七十七';echo charN2intN($temp)."\t".$temp;echo "\n";echo "\n";echo "solve_discrete:false,只处理连续的数\n";echo "\n";echo "通过的正常格式\n";$temp = '从七开始';echo charN2intN($temp,false)."\t\t\t".$temp;echo "\n";$temp = '从十开始';echo charN2intN($temp,false)."\t\t\t".$temp;echo "\n";$temp = '从十七开始';echo charN2intN($temp,false)."\t\t\t".$temp;echo "\n";$temp = '从七十开始';echo charN2intN($temp,false)."\t\t\t".$temp;echo "\n";$temp = '从七十七开始';echo charN2intN($temp,false)."\t\t\t".$temp;echo "\n";$temp = '从七佰零七开始';echo charN2intN($temp,false)."\t\t\t".$temp;echo "\n";$temp = '从七佰一十七开始';echo charN2intN($temp,false)."\t\t\t".$temp;echo "\n";$temp = '从七佰七十七开始';echo charN2intN($temp,false)."\t\t\t".$temp;echo "\n";$temp = '从七千零七开始';echo charN2intN($temp,false)."\t\t".$temp;echo "\n";$temp = '从七千零七十七开始';echo charN2intN($temp,false)."\t\t".$temp;echo "\n";$temp = '从七千七佰零七开始';echo charN2intN($temp,false)."\t\t".$temp;echo "\n";$temp = '从七千七佰七十七开始';echo charN2intN($temp,false)."\t\t".$temp;echo "\n";echo "\n";echo "通过的非正常格式\n";$temp = '从七千零七十七开始到12';echo charN2intN($temp,false)."\t\t".$temp;echo "\n";$temp = '从一千四百八十七开始到12';echo charN2intN($temp,false)."\t\t".$temp;echo "\n";$temp = '从五七开始';echo charN2intN($temp,false)."\t\t\t".$temp;echo "\n";$temp = '从五七开始到七零七零零';echo charN2intN($temp,false)."\t\t\t".$temp;echo "\n";$temp = '从五七开始到七零七00';echo charN2intN($temp,false)."\t\t\t".$temp;echo "\n";$temp = '从203开始到七零七';echo charN2intN($temp,false)."\t\t\t".$temp;echo "\n";$temp = '从203开始到707';echo charN2intN($temp,false)."\t\t\t".$temp;echo "\n";echo "\n";
测试结果
0 0
- 获取中文字符串中的数字并转换成阿拉伯数字
- 将字符串中的中文数字转换成阿拉伯数字
- 中文数字转换成阿拉伯数字。
- 将中文数字转换成阿拉伯数字
- 阿拉伯数字转换成中文大写数字
- java中文数字转换成阿拉伯数字
- java 中文大写数字 转换成 阿拉伯数字
- Java实现阿拉伯数字转换成中文大写数字,以及中文大写数字到阿拉伯数字的转换。
- Java实现阿拉伯数字转换成中文大写数字,以及中文大写数字到阿拉伯数字的转换
- 阿拉伯数字转换为中文数字
- 阿拉伯数字转换为中文数字
- 数字转换工具类:将阿拉伯数字转换成中文
- 阿拉伯数字表示的数字转换成中文习惯读法
- 把一个阿拉伯数字转换成中文数字的算法
- Java-String-阿拉伯数字转换成中文汉字数字
- 很详细的:java中文数字转换成阿拉伯数字
- Java编程案例-将阿拉伯数字转换成中文数字
- 将一组阿拉伯数字转换成中文大写数字
- Linux运行等级,根目录,文件类型etc
- 不同类型员工存入数组打印某月工资
- 枚举类型(enum)
- 荒岛求生第一关
- 行车记录仪开发方案比较--转载
- 获取中文字符串中的数字并转换成阿拉伯数字
- CentOS安装XRDP实现远程桌面访问
- post和get对参数的异同
- 用户输入
- JSP工作原理
- tcpdump抓包分析TCP三次握手过程
- git提交pull request到主项目
- Calendar 时间API
- centos下安装ZooKeeper