Home Backend Development PHP Tutorial PHP realizes the conversion of Chinese characters into integer numbers, such as: one hundred and one is converted into 101

PHP realizes the conversion of Chinese characters into integer numbers, such as: one hundred and one is converted into 101

Jul 25, 2016 am 08:45 AM

  1. test();
  2. /**
  3. * Test
  4. */
  5. function test() {
  6. echo CnToInt('一'); // 1
  7. echo CnToInt('十'); // 10
  8. echo CnToInt ('Eleven'); // 11
  9. echo CnToInt('One hundred and ten'); // 110
  10. echo CnToInt('One thousand and one'); // 1001
  11. echo CnToInt('One thousand and one hundred' Zero one'); // 10101
  12. echo CnToInt('one hundred and thirteen million and three thousand and one'); // 113003001
  13. echo CnToInt('one quadrillion'); // 11.0E+15
  14. }
  15. /**
  16. * Chinese to number
  17. * @param String $var Chinese number to be parsed
  18. * @param Int $start initial value
  19. * @return int
  20. */
  21. function CnToInt($var, $start = 0) {
  22. if (is_numeric($var)) {
  23. return $var;
  24. }
  25. if (intval($var) = == 0) {
  26. $splits = array('100 million' => 100000000, '10,000' => 10000);
  27. $chars = array('10,000' => 10000, '1000' => 1000, 'hundred' => 100, '十' => 10, '一' => 1, 'zero' => 0);
  28. $Ints = array('zero' => 0, '一' => 1, '二' => 2, '三' => 3, '四' => 4, '五' => 5, '六' => 6, '七' =&gt ; 7, '八' => 8, '九' => 9, '十' => 10);
  29. $var = str_replace('zero', "", $var);
  30. foreach ($splits as $key => $step) {
  31. if (strpos($var, $key)) {
  32. $strs = explode($key, $var);
  33. $start += CnToInt(array_shift($strs)) * $step;
  34. $var = join('', $strs);
  35. }
  36. }
  37. foreach ($chars as $key => $step) {
  38. if (strpos($var, $key) !== FALSE ) {
  39. $vs = explode($key, $var);
  40. if ($vs[0] === "") {
  41. $vs[0] = '一';
  42. }
  43. $start += $Ints [array_shift($vs)] * $step;
  44. $var = join('', $vs);
  45. } elseif (mb_strlen($var, 'utf-8') === 1) {
  46. $start += $Ints[$var];
  47. $var = '';
  48. break;
  49. }
  50. }
  51. return $start;
  52. } else {
  53. return intval($var);
  54. }
  55. }
Copy code

Convert to, PHP, one hundred and one


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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles