PHP converts Chinese characters to integer numbers, for example, one hundred and one to 101.

Source: Internet
Author: User
PHP converts Chinese characters to integer numbers, for example, one hundred and one to 101.

  1. Test ();
  2. /**
  3. * Test
  4. */
  5. Function test (){
  6. Echo CnToInt ('1'); // 1
  7. Echo CnToInt ('10'); // 10
  8. Echo CnToInt ('11'); // 11
  9. Echo CnToInt ('20140901'); // One hundred and ten
  10. Echo CnToInt ('20140901'); // One Thousand and One
  11. Echo CnToInt ('20140901'); // ten thousand one hundred and one
  12. Echo CnToInt ('2014, 0.1 billion 111111'); // 13,003,001
  13. Echo CnToInt ('20140901'); // 11.0E + 15
  14. }
  15. /**
  16. * Convert Chinese to numbers
  17. * @ Param String $ number of Chinese characters to be parsed by var
  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 (' '=> 100000000, 'wan' => 10000 );
  27. $ Chars = array ('wan' => 10000, 'kilobytes => 1000, '100' => 100, '10' => 10, '1' => 1, '0' => 0 );
  28. $ Ints = array ('0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, '10' => 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] = '1 ';
  42. }
  43. $ Start + = $ Ints [array_shift ($ vs)] * $ step;
  44. $ Var = join ('', $ );
  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. }


Converted to PHP, One Hundred and One

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.