Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to transfer php Chinese characters to Arabic numerals

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to convert php Chinese characters to Arabic numerals". In daily operation, I believe many people have doubts about how to transfer php Chinese characters to Arabic numerals. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to convert php Chinese characters to Arabic numerals". Next, please follow the editor to study!

The method of php Chinese character to Arabic number: 1, create a PHP example file; 2, initialize an array; 3, divide the string into units; 4, convert the grouped string into numbers and multiply it by units.

This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer

How to transfer php Chinese characters to Arabic numerals?

Php Chinese to Arabic numerals

At that time, I wrote an interview question full of loopholes and was despised, so I came back and wrote one all morning. I tested it myself, and it seemed OK.

Train of thought

Such as Chinese numbers

Seven hundred and eighty-four hundred and seventy-eight thousand four hundred eighty-six

If the red text is regarded as a unit, then after dividing with these units, the rest of the text is a hundred thousand numbers. Multiply these numbers by the sum of their units, and that's the result.

Code

Function ch3num ($str) {/ / Units array is used for loop traversal, unit order from large to small $c = ['trillion' = > 10000000000, 'billion' = > 100000000, 'ten thousand' = > 10000,]; / / Chinese replacement number rule, zero is of no use So remove $b = ['one' = > 1,'2'= > 2,'3'= > 3,'4'= > 4,'5'= > 5,'6'= > 6,'7'= > 7,'8'= > 8,'9'= > 9, 'zero' = >',] / / replace the number $str = str_replace (array_keys ($b), array_values ($b), $str); / / result 778,0421,751,744,1008 / / if the string begins with a ten, precede it with 1 if (mb_strpos ($str,'x, 0, 'utf-8') = = 0) $str =' 1'.$str / / initialize an array $arr [] = array ('str' = > $str, / / string' unit' = > 1, / / unit); / / split the string by unit 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 } / * result ([0] = > Array ([str] = > 7 million + 8 [unit] = > 1000000000000) [1] = > Array ([str] = > 47002 + [unit] = > 100000000 ) [2] = > Array ([str] = > 7507 + [unit] = > 10000) [3] = > Array ([str] = > 44008 + 6 [unit] = > 1)) * / / / traversal summation $sum = 0 Foreach ($arr as $item) {$sum + = getNum ($item ['str'], $item [' unit']);} return $sum;} / / converts the grouped string into a number and multiplies it by the unit function getNum ($str,$st) {/ / multiple $a = ['thousand' = > 1000, 'hundred' = > 1000, 'ten' = > 10]; / / the starting value $num = 0 / / the number of digits of the current value $step = 1; / / Unit $un = 1; $arr = preg_split ('/ (?

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report