How to convert Chinese characters to Arabic numerals in php

藏色散人
Release: 2023-03-13 16:06:01
Original
1450 people have browsed it

php Method to convert Chinese characters to Arabic numbers: 1. Create a PHP sample file; 2. Initialize an array; 3. Split the string into units; 4. Convert the grouped strings into numbers and Just multiply by the unit.

How to convert Chinese characters to Arabic numerals in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

php How to convert Chinese characters to Arabic numerals?

php Chinese to Arabic numerals

Interview question, I wrote one that was full of loopholes and was despised by others, so I came back and spent the whole morning writing one, I tested it myself and it seems to be OK.

Thoughts

Such as Chinese numbers

Seven hundred and seventy-eight trillion, forty-seven trillion, seventy-five million, seven hundred and four One Thousand Four Hundred and Eighty-Six

If the red text is regarded as units, then after dividing it by these units, the other parts will be a number of ten hundred thousand. Multiply these numbers by their units and sum them up to get the result.

Code

function ch2num($str){
    //单位数组用于循环遍历,单位顺序从大到小
    $c = [
        '万亿'=>1000000000000,
        '亿' => 100000000,
        '万' => 10000,
    ];
    //中文替换数字规则,零没什么卵用;所以去掉
    $b = [
        '一' =>1,
        '二' =>2,    
        '三' =>3,
        '四' =>4,
        '五' =>5,
        '六' =>6,
        '七' =>7,
        '八' =>8,
        '九' =>9,
        '零' =>'',
    ];
    //替换数字
    $str =  str_replace(array_keys($b), array_values($b), $str);
    //结果 7百7十8万亿4千7百2十亿7千5百7十万4千4百8十
  //如果字符串以十开头,前边加1
    if(mb_strpos($str, '十' ,0 ,'utf-8') === 0)
        $str = '1'.$str;
      
    //初始化一个数组
    $arr[] = array(
        'str'    => $str, //字符串
        'unit'  => 1,     //单位
    );
    //将字符串按单位切分
    foreach ($c as $key => $value) {
        $brr = [];
        foreach ($arr as $item) {
            if(strpos($item['str'], $key)){
                $sun = explode($key, $item['str'],2);
                $brr[] = [
                    'str' => $sun[0],
                    'unit' => $value,
                ];
                $brr[] = [
                    'str' => $sun[1],
                    'unit' => $item['unit'],
                ];
            }else{
                $brr[] = $item;
            }
        }
        $arr = $brr;
    }
    /*    结果
        (
        [0] => Array
            (
                [str] => 7百7十8
                [unit] => 1000000000000
            )
        [1] => Array
            (
                [str] => 4千7百2十
                [unit] => 100000000
            )
        [2] => Array
            (
                [str] => 7千5百7十
                [unit] => 10000
            )
        [3] => Array
            (
                [str] => 4千4百8十6
                [unit] => 1
            )
        )*/
    //遍历求和
    $sum = 0;
    foreach ($arr as $item) {
        $sum += getNum($item['str'],$item['unit']);
    }
    return $sum;
}
//将分组后的字符串转化成数字,并乘以单位
function getNum($str,$st){
    //倍数
    $a = [
        '千'=>1000,
        '百'=>100,
        '十'=>10
    ];
    //开始值
    $num = 0;
    //当前值所在位数
    $step = 1;
    //单位
    $un = 1;
    $arr =  preg_split(&#39;/(?<!^)(?!$)/u&#39;, $str);
    while (count($arr)) {
        $m = array_pop($arr);
        //如果是位数;更新倍数
        if(!empty($a[$m])){
            $step = $a[$m];
        }
        if(is_numeric($m)){
            $num += $m * $step;
        }
    }
    return $num * $st;
}
Copy after login

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to convert Chinese characters to Arabic numerals in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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