php中文转阿拉伯

180it 2019-12-11 PM 1719℃ 0条

/**

  • 中文转阿拉伯
  • @param $str
  • @return float|int|mixed
    */

function chrtonum($str)
{

$num = 0;
$bins = array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九", 'a' => "个", 'b' => "十", 'c' => "百", 'd' => "千", 'e' => "万");
$bits = array('a' => 1, 'b' => 10, 'c' => 100, 'd' => 1000, 'e' => 10000);
foreach ($bins as $key => $val) {
    if (strpos(" " . $str, $val)) $str = str_replace($val, $key, $str);
}
foreach (str_split($str, 2) as $val) {
    $temp = str_split($val, 1);
    if (count($temp) == 1) $temp[1] = "a";
    if (isset($bits[$temp[0]])) {
        $num = $bits[$temp[0]] + (int)$temp[1];
    } else {
        $num += (int)$temp[0] * $bits[$temp[1]];
    }
}
return $num;

}

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

php中文转阿拉伯