Home > Backend Development > PHP Tutorial > Chinese character code php number to Chinese character code algorithm

Chinese character code php number to Chinese character code algorithm

WBOY
Release: 2016-07-29 08:46:57
Original
1238 people have browsed it

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template