Php converts Chinese characters to numbers, and the implementation method is very intelligent,

Source: Internet
Author: User

Php converts Chinese characters to numbers, and the implementation method is very intelligent,

Share an auxiliary function and use php to identify the numbers in the string as much as possible. The effect is as follows.

123456789 echo checkNatInt('123'); // Common Chinese number, 9600000echo checkNatInt('Hundreds of millions of users can pick it up with zero scale'); // Uppercase Chinese number, 170000840echo checkNatInt('The annual salary of one million for learning PHP'); // Number in the sentence, 1000000echo checkNatInt('The number of people sent this Spring Festival reaches 2.6 billion'); // Mix numbers and Chinese characters, 2600000000 echo checkNatInt('Two bees'); // Colloquial number, 2echo checkNatInt('Never expected'); // Retro expression, 100000000

 

Notes

  1. Decimal places not supported

  2. PHP_INT_MAX is not supported.

 

Partial implementation code

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 function checkNatInt($str) {    $map array(        '1' => '1','2' => '2','3' => '3','4' => '4','5' => '5','6' => '6','7' => '7','8' => '8','9' => '9',        'Yi' => '1','Er' => '2','San' => '3','Ta' => '4','Wo' => '5','Loan' => '6','Authorization' => '7','Authorization' => '8','Authorization' => '9',        '0' => '0','Two' => '2',        'Authorization' => 'Kilo','Authorization' => 'Bai','Pick' => '10',        'Ten thousand' => 'Yi',    );       $str str_replace(array_keys($map), array_values($map), $str);    $str = checkString($str'/([\ D hundreds of millions] +)/U');     $func_c2i function ($str$plus = false) use(&$func_c2i) {        if(false === $plus) {            $plus array('Yi' => 100000000,'Wan' => 10000,'Kilo' => 1000,'Bai' => 100,'10' => 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  function 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;}

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.