Copy the code The code is as follows:
//Convert numbers to Chinese characters, for example, 1210 is converted to one thousand two hundred and ten
$num = "842105580";//Nine digits
function del0($num ) //Remove the 0 in front of the number field
{
return "".intval($num);
}
function n2c($x) //Convert a single number to a Chinese character
{
$arr_n = array("zero"," one","two","three","four","five","six","seven","eight","nine","ten");
return $arr_n[$x];
}
function num_r($abcd) //Read value (4 digits)
{
$arr= array();
$str = ""; //Read Chinese character value
$flag = 0; // Whether the bit is zero
$flag_end = 1; //Whether it ends 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]); //Turn Chinese characters from back to front
echo $ch;
echo "";
if($ch == "zero" && $flag == 0){ / /If it is the first zero
$flag = 1; //This bit is zero
$str = $ch.$str; //Add Chinese character numerical string
continue;
}elseif($ch == "zero" ){ //If it is not the first zero
continue;
}
$flag = 0; //The bit is not zero
switch($j) {
case 0: $str = $ch; $flag_end = 0; break; //The first digit (the end) does not end with "zero"
case 1: $str = $ch."十".$str; break; //The second digit
case 2: $str = $ch ."hundred".$str; break; //The third digit
case 3: $str = $ch."千".$str; break; //The 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 the "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 it reaches nine digits, read the "100 million" digit
{
$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))."万";
$c = num_r(del0($y))."Billion";
}elseif($numlen <= 8 && $numlen > ;= 5) //If it is greater than or equal to "ten thousand"
{
$wsbq = substr($num_real, 0, $numlen-4);
$gsbq = substr($num_real, -4);
$a = num_r( del0($gsbq));
$b = num_r(del0($wsbq))."万";
$c="";
}elseif($numlen <= 4) //If it is less than or equal to "thousand"
{
$gsbq = substr( $num_real, -$numlen);
$a = num_r(del0($gsbq));
$b="";
$c="";
}
$ch_num = $ c.$b.$a;
return $ch_num;
}
echo $num.""; //Numbers
echo num2ch($num); //Chinese characters
echo "";
echo num2ch("1240") ;
The above introduces the Chinese character code PHP digital to Chinese character code algorithm, including the content of Chinese character code. I hope it will be helpful to friends who are interested in PHP tutorials.