PHP for Chinese to Digital, _php tutorial

Source: Internet
Author: User

PHP implements Chinese to digital,


Share an auxiliary function, using PHP to identify as many numbers as possible in the string,

First on the code

function Checknatint ($str) {$map = array (' One ' = ' 1 ', ' two ' = ' 2 ', ' three ' = ' 3 ', ' four ' = ' 4 ', ' five ' = ' 5 ', ' VI ' = > ' 6 ', ' seven ' = ' 7 ', ' eight ' = ' 8 ', ' nine ' = ' 9 ', ' One ' and ' 1 ', ' II ' and ' 2 ', ' three ' = ' 3 ', ' ' ' ' and ' 4 ', ' Wu ' and ' 5 ', ' Land ' and ' 6 ', ' qi ' and ' 7 ', ' ba ' = ' 8 ', ' nine ' = ' 9 ', ' 0 ' = ' 0 ', ' two ' = ' 2 ', ' thousand ' = ' thousand ', ' bai ' and ' hundred '    , ' pick ' up ' = ' ten ', ' absolute ' = ' billion ', ';  $str = Str_replace (Array_keys ($map), Array_values ($map), $str);   $str = checkstring ($str, '/([\d million Baishi]+)/U '); $func _c2i = function ($str, $plus = False) use (& $func _c2i) {if (false = = = $plus) {$plus = array (' billion ' + 100    000000, ' million ' and 10000, ' thousand ' = 1000, ' hundred ' = 100, ' Ten ' + 10, ';    } $i = 0;      if ($plus) foreach ($plus as $k = + $v) {$i + +;         if (Strpos ($str, $k)!== false) {$ex = explode ($k, $STR, 2);        $new _plus = Array_slice ($plus, $i, NULL, TRUE);        $l = $func _c2i ($ex [0], $new _plus); $r = $func _c2i ($ex [1], $new _plus);        if ($l = = 0) $l = 1;      Return $l * $v + $r;  }} return (int) $str; } return $func _c2i ($STR);    }//From the UCT PHP development Framework, where the checkstring function is as follows functions Checkstring ($var, $check = ", $default =") {if (!is_string ($var)) {    if (Is_numeric ($var)) {$var = (string) $var;    } else {return $default;  }} if ($check) {return (Preg_match ($check, $var, $ret)? $ret [1]: $default); } return $var;}

The implementation results are as follows.

echo checknatint (' 9.6 million '); Ordinary Chinese numerals, 9600000echo checknatint (' Yi billion qi thousand million Ba Bai restaurant pick up '); Uppercase Chinese numerals, 170000840  

Precautions

Decimals not supported

Numbers greater than Php_int_max are not supported

Articles you may be interested in:

    • PHP Chinese alphanumeric Verification Code implementation codes
    • The function of an Arabic numeral to Chinese number under PHP
    • Support Chinese alphanumeric, custom font PHP CAPTCHA code
    • PHP RMB amount to Chinese capitalization function code
    • PHP to determine if it is a Chinese/english/Numeric sample code
    • Php+ the alphanumeric and Chinese characters in the string are divided
    • Support for Chinese, Alphabet, and digital PHP verification code
    • PHP implements the Chinese and numeric instances in the filter string

http://www.bkjia.com/PHPjc/1100912.html www.bkjia.com true http://www.bkjia.com/PHPjc/1100912.html techarticle PHP realizes the Chinese to the number, shares an auxiliary function, uses PHP to recognize as far as possible the number in the string, first on the code function checknatint ($str) {$map = array (' One ' = ' 1 ', ' two ' ...

  • 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.