The function algorithm of PHP number word to Chinese character

Source: Internet
Author: User
The function algorithm of PHP digital to Chinese character

Functions of PHP Digital to Chinese characters

/********************* Digital to Chinese character ***********************/function del0 ($num)//Remove 0{return "" in front of the number field. 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 bits) {$arr = array (); $str = ""; The value of Chinese characters after reading is $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]);//From backward forward to Chinese characters//echo $ch; echo "
"; if ($ch = = "0" && $flag = = 0) {//If it is the first 0 $flag = 1;//The bit is zero $str = $ch. $str;//Add Chinese character value string Continue }elseif ($ch = = "0") {//If it is not the first zero continue; } $flag = 0; This bit is not 0 switch ($j) {case 0: $str = $ch; $flag _end = 0; Break The first bit (end), without ending with "0" Case 1: $str = $ch. " Ten ". $str; Break Second Case 2: $str = $ch. " Hundred ". $str; Break Third Case 3: $str = $ch. " Thousand ". $str; Break Fourth bit} if ($flag _end = = 1)//If End with "0" {mb_internal_encoding ("UTF-8"); $str = mb_substr ($str, 0, Mb_strlen ($str)-1); Remove the "0"}if ($arrlen = = 2 && $arr [0] = = 1) {$str = substr ($str, 2);} return $STR;} function num2ch ($num)//Overall read conversion {$num _real = del0 ($num);//Remove the preceding "0" $numlen = strlen ($num _real);//echo "numlen=". $num Len. "
"; if ($numlen >= 9)//If full nine bits, read "million" 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 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;} /****************** Digit to Chinese character end ********************/
?
  • 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.