Home >Backend Development >PHP Problem >How to convert uppercase numbers to Arabic numerals in php
How to convert uppercase numbers to Arabic numerals in php: [function chinese2number($chNum) { $numArr = array('zero' => '0','one' => '1','two' => '2','three' => ...】.
The operating environment of this article: windows10 system, php 7, thinkpad t480 computer.
Due to work needs, we may need to convert uppercase numbers into corresponding Arabic Numbers, many friends may not have studied this issue, so they don’t know how to write them for a while. Let’s take a look at how to convert uppercase numbers into corresponding Arabic numerals.
Code implementation:
function chinese2number($chNum) { $numArr = array( '零' => '0', '一' => '1', '二' => '2', '三' => '3', '四' => '4', '五' => '5', '六' => '6', '七' => '7', '八' => '8', '九' => '9', ); $numUnit = array( '十' =>'10', '百' =>'100', '千' =>'1000', '万' =>'10000', '亿' =>'100000000', ); $number = 0; for ($i = 0;$i<=mb_strlen($chNum); $i++) { $str = mb_substr($chNum,$i,2); $first = mb_substr($str,0,1); $second = mb_substr($str,1,1); if (isset($numUnit[$first])){ if (!$number) { $number = 1; } $number *= $numUnit[$first]; } else if (isset($numArr[$first])) { $i++; if (isset($numArr[$second])) { if ($numArr[$first] == 0) { $i--; }else{ $number += intval($numArr[$first].$numArr[$second]); } } else if(isset($numUnit[$second])) { $number += intval($numArr[$first]*$numUnit[$second]); } else{ $number += intval($numArr[$first]); } } } return $number; }
Recommended learning: php training
The above is the detailed content of How to convert uppercase numbers to Arabic numerals in php. For more information, please follow other related articles on the PHP Chinese website!