How to convert uppercase numbers to Arabic numerals in php

王林
Release: 2023-03-12 16:42:01
Original
2544 people have browsed it

How to convert uppercase numbers to Arabic numerals in php: [function chinese2number($chNum) { $numArr = array('zero' => '0','one' => '1','two' => '2','three' => ...】.

How to convert uppercase numbers to Arabic numerals in php

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;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!