Home > Backend Development > PHP Tutorial > PHP number to Chinese character code (algorithm)_PHP tutorial

PHP number to Chinese character code (algorithm)_PHP tutorial

WBOY
Release: 2016-07-21 15:24:13
Original
1013 people have browsed it

Copy code The code is as follows:

//Convert numbers to Chinese characters, such as 1210 to one thousand two hundred and ten
$num = "842105580";//Nine-digit number
function del0($num) //Remove the 0 in front of the number field
{
return "".intval($num);
}
function n2c($x) //A single number changes 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 = ""; //The 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]); //Convert 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; //This bit is not zero
switch($j) {
case 0: $str = $ch; $flag_end = 0; break; //The first bit (last) , 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 "zero"
}
return $str;
}
function num2ch($num) //Overall read conversion
{
$num_real = del0($ num);//Remove the leading "0"
$numlen = strlen($num_real);
echo "numlen=".$numlen."";
if($numlen >= 9) //If it reaches nine digits, read the "billion" digits
{
$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))."Million";
$c = num_r(del0($y))."Billion";
}elseif($numlen <= 8 && $numlen >= 5) // If 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 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."" ; //Number
echo num2ch($num); //Chinese characters
echo "";
echo num2ch("1240");

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324379.htmlTechArticleCopy code The code is as follows: //Convert numbers to Chinese characters, such as 1210 to one thousand two hundred and ten$ num = "842105580";//Nine-digit function del0($num) //Remove the 0 in front of the number field { retur...
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