Php code for converting numbers into Chinese characters (algorithm ). Copy the code as follows: convert a number to a Chinese character, for example, 1210 to one thousand two hundred and ten $ num842105580; remove the value 0 {retur before the number segment from the nine-digit functiondel0 ($ num)
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 ");
The http://www.bkjia.com/PHPjc/324379.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/324379.htmlTechArticle 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 the 0 {retur...