PHP implementation of the number of Chinese characters, such as: 101 Turn 101

Source: Internet
Author: User
  1. Test ();
  2. /**
  3. * Test
  4. */
  5. function Test () {
  6. Echo cntoint (' one '); 1
  7. Echo cntoint (' ten '); 10
  8. Echo cntoint (' 11 '); 11
  9. Echo cntoint (' 110 '); 110
  10. echo cntoint (' 1001 '); 1001
  11. echo cntoint (' 10,101 '); 10101
  12. echo cntoint (' 113,003,001 '); 113003001
  13. echo cntoint (' 1000 trillion '); 11.0E+15
  14. }
  15. /**
  16. * Chinese to Digital
  17. * @param String $var The number of Chinese to parse
  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 (' billion ' = + 100000000, ' million ' + 10000);
  27. $chars = Array (' million ' = 10000, ' thousand ' = 1000, ' hundred ' = 100, ' ten ' = 10, ' One ' + 1, ' 0 ' = 0);
  28. $Ints = Array (' 0 ' = + 0, ' one ' = 1, ' two ' = 2, ' three ' = ' 3, ' four ' = 4, ' Five ' = 5, ' six ' = ' 6 ', ' seven ' = ' 7 ', ' eight ' = 8, ' Nine ' = 9, ' Ten ' = 10 ';
  29. $var = Str_replace (' 0 ', "", $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] = ' one ';
  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
Turn, PHP, 101
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.