PHP Chinese capital number to digital code

Source: Internet
Author: User
Tags php tutorial sprintf strlen

PHP tutorial Chinese capital numbers into digital code

echo "<pre>";
echo Test (' 1,200,710,051 ');
/*
Create by Piaofen
Date:2011-03-31
Function: Chinese capital numerals
*/
function Chtonum ($str = ')
{
$unit = Array (' Billion ' =>100000000, ' million ' =>10000, ' thousand ' =>1000, ' thousand ' =>1000, ' hundred ' =>100, ' ten ' =>10);
$num = Array (' One ' =>1, ' two ' =>2, ' three ' =>3, ' four ' =>4, ' five ' =>5, ' Six ' =>6, ' seven ' =>7, ' eight ' =>8, ' nine ' =>9);
$str = Str_replace (Array_keys ($num), $num, $STR);
$result = Array ();
$number = ';
Preg_match_all ('/[0-9] thousand [0-9] hundred [0-9] ten [0-9]|[ 0-9] hundred [0-9] ten [0-9]| [0-9] Ten [0-9]| [0-9]/ism ', $str, $pnum);
foreach ($pnum [0] as $val) {
$tmp = ';
for ($i =0; $i <mb_strlen ($val, ' utf-8 '); $i + +) {
$s = Mb_substr ($val, $i, 1, ' utf-8 ');
if (!is_numeric ($s)) {
$k = $unit [$s];
if (strlen ($tmp) >=strlen ($k)) {
Preg_match ('/[0-9]* ') ([0-9]{]. ( Strlen ($k)-1). '} ([0-9])/ism ', $tmp, $n);
$tmp = ($n [1]+ $n [3]). $n [2];
}else{
$tmp = $tmp * $K;
}
}else if ($i = = (Mb_strlen ($val, ' Utf-8 ')-1)) {
$tmp + + $s;
}else{
$tmp. = $s;
}
}
$nnum [] = $tmp;
}
$result = Str_replace (Array_keys ($unit), '; ', Str_replace ($pnum [0], $nnum, $str));
foreach (Explode ('; ', $result) as $val) {
$number. = sprintf ('%04d ', $val);
}
return sprintf ('%2u ', $number);
}

Related Article

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.