PHP Digital Conversion Chinese character code (algorithm) _php tips

Source: Internet
Author: User
Copy Code code as follows:

Converts a number to Chinese characters, such as 1210 to 1210
$num = "842105580";//Nine digits
function Del0 ($num)//Remove 0 before the number segment
{
Return "". Intval ($num);
}
function n2c ($x)//single digit variable kanji
{
$arr _n = Array ("0", "one", "two", "three", "four", "five", "six", "seven", "eight", "Nine", "ten");
return $arr _n[$x];
}
function Num_r ($ABCD)//Read value (4-bit)
{
$arr = Array ();
$str = ""; Number of Chinese characters after reading
$flag = 0; Whether the bit is zero
$flag _end = 1; Whether to end with "0"
$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-$j]); Turn the characters from the back forward
echo $ch;
echo "";
if ($ch = = "0" && $flag = = 0) {//if the first 0
$flag = 1; The bit is zero
$str = $ch. $str; Add Chinese character value string
Continue
}elseif ($ch = = "0") {//If not the first zero
Continue
}
$flag = 0; The bit is not 0
Switch ($j) {
Case 0: $str = $ch; $flag _end = 0; Break First (end), not ending with "0"
Case 1: $str = $ch. " X. $STR; Break Second place
Case 2: $str = $ch. " Hundred ". $str; Break Third place
Case 3: $str = $ch. " Thousand ". $str; Break Fourth place
}
}
if ($flag _end = = 1)//If ending with "0"
{
Mb_internal_encoding ("UTF-8");
$str = mb_substr ($str, 0, Mb_strlen ($str)-1); Get rid of "0."
}
return $str;
}
function num2ch ($num)//overall read conversion
{
$num _real = del0 ($num);//Remove front "0"
$numlen = strlen ($num _real);
echo "numlen=". $numlen. "";
if ($numlen >= 9)//If full nine digits, read "billion" bit
{
$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 "million"
{
$WSBQ = substr ($num _real, 0, $numlen-4);
$GSBQ = substr ($num _real,-4);
$a = Num_r (Del0 ($GSBQ));
$b = Num_r (Del0 ($WSBQ)). " Million ";
$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. ""; Digital
echo num2ch ($num); Chinese characters
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.