Php code for converting numbers to Chinese characters (algorithm)

Source: Internet
Author: User
Php code for converting numbers to Chinese characters (algorithms). For more information about php learning, see. The code is as follows:
// Convert a number to a Chinese character, for example, 1210 to one thousand two hundred and ten
$ Num = "842105580"; // nine-digit
Function del0 ($ num) // Remove 0 from the front of the number segment
{
Return "". intval ($ num );
}
Function n2c ($ x) // change a single digit to a Chinese character
{
$ Arr_n = array ("zero", "one", "two", "three", "four", "five", "six", "seven ", "8", "9", "10 ");
Return $ arr_n [$ x];
}
Function num_r ($ abcd) // read the Value (4 digits)
{
$ Arr = array ();
$ Str = ""; // Chinese character value after reading
$ Flag = 0; // whether the bit is zero
$ Flag_end = 1; // whether to end with "zero"
$ Size_r = strlen ($ abcd );
For ($ I = 0; $ I <$ size_r; $ I ++)
{
$ Arr [$ I] = $ abcd {$ I };
}
$ Arrlen = count ($ arr );
For ($ j = 0; $ j <$ arrlen; $ j ++)
{
$ Ch = n2c ($ arr [$ arrlen-1-$ j]); // Convert Chinese characters from the back to the front
Echo $ ch;
Echo "";
If ($ ch = "zero" & $ flag = 0) {// if it is the first zero
$ Flag = 1; // The bit is zero.
$ Str = $ ch. $ str; // add a Chinese character numeric string
Continue;
} Elseif ($ ch = "zero") {// if it is not the first zero
Continue;
}
$ Flag = 0; // This bit is not zero
Switch ($ j ){
Case 0: $ str = $ ch; $ flag_end = 0; break; // The first (end), not ending with "zero"
Case 1: $ str = $ ch. "Ten". $ str; break; // second digit
Case 2: $ str = $ ch. "hundred". $ str; break; // The third digit
Case 3: $ str = $ ch. "thousand". $ str; break; // fourth digit
}
}
If ($ flag_end = 1) // if it ends with "zero"
{
Mb_internal_encoding ("UTF-8 ");
$ Str = mb_substr ($ str, 0, mb_strlen ($ str)-1); // remove "zero"
}
Return $ str;
}
Function num2ch ($ num) // overall read conversion
{
$ Num_real = del0 ($ num); // remove the preceding "0"
$ Numlen = strlen ($ num_real );
Echo "numlen =". $ numlen ."";
If ($ numlen> = 9) // if the number is nine digits, read the "" bits.
{
$ Y = substr ($ num_real,-9, 1 );
// Echo $ y;
$ Wsbq = substr ($ num_real,-8, 4 );
$ Gsbq = substr ($ num_real,-4 );
$ A = num_r (del0 ($ gsbq ));
$ B = num_r (del0 ($ wsbq). "Ten Thousand ";
$ C = num_r (del0 ($ y). "";
} Elseif ($ numlen <= 8 & $ numlen> = 5) // if the value is greater than or equal"
{
$ Wsbq = substr ($ num_real, 0, $ numlen-4 );
$ Gsbq = substr ($ num_real,-4 );
$ A = num_r (del0 ($ gsbq ));
$ B = num_r (del0 ($ wsbq). "Ten Thousand ";
$ C = "";
} Elseif ($ numlen <= 4) // if the value is less than or equal to "thousands"
{
$ Gsbq = substr ($ num_real,-$ numlen );
$ A = num_r (del0 ($ gsbq ));
$ B = "";
$ C = "";
}
$ Ch_num = $ c. $ B. $;
Return $ ch_num;
}
Echo $ num. ""; // number
Echo num2ch ($ num); // Chinese character
Echo "";
Echo num2ch ("1240 ");

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.