将数字字符串转换成数字

前端之家收集整理的这篇文章主要介绍了将数字字符串转换成数字前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP">$digit = ['亿' => 100000000,'万' => 10000];
$data_explode = [];
$cardinal = 0;
foreach ($digit as $key => $value) {
    if (strpos($data,$key)) {
        $data = explode($key,$data);
        $data_explode[$key] = $data[0];
        $data = $data[1];
    }

    if ($key == '万') {
        $data_explode['个'] = $data;
    }
}

$result = 0;
foreach ($data_explode as $key => $value) {
    $data = ltrim($value,'零');

    if (strpos($data,'零')) { //存在零 
        $data = str_replace('零','',$data);
    }

    $cardinal = 1;
    $int = 0;
    $qian = mb_strpos($data,'千');
    if ($qian !== false) { //存在千
        $refer = mb_substr($data,$qian - 1,1,'utf8');
        $refer = in_array($refer,$str) * 1000;
        $int += $refer;
        $data = mb_substr($data,1 + 1);
    }
    $bai = mb_strpos($data,'百');
    if ($bai !== false) { //存在百
        $refer = mb_substr($data,$bai - 1,'utf8');
        $refer = array_search($refer,$str) * 100;
        $int += $refer;
        $data = mb_substr($data,1 + 1);
    }
    $shi = mb_strpos($data,'十');
    if ($shi !== false) { //存在十
        $refer = mb_substr($data,$shi - 1,$str) * 10;
        $int += $refer;
        $data = mb_substr($data,1 + 1);
    }

    if ($data) {
        $refer = array_search($data,$str);
        $int += $refer;
    }

    $result += $int * (isset($digit[$key]) ? $digit[$key] : 1);
} 
echo $result;

}

str2int('一亿零一百');
// str2int('一千零二十三');
?>

猜你在找的程序笔记相关文章