获取中文字符串中的数字并转换成阿拉伯数字

该博客介绍如何从中文字符串中提取并转换数字到阿拉伯数字的形式,提供了测试结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获取中文字符串中的数字并转换成阿拉伯数字

<?php
function charN2intN($str, $solve_discrete = true)
{
    //个位数字
    $arr_char_unit = array('1一壹','2二贰两','3三叁','4四肆','5五伍','6六陆','7七柒','8八捌','9九玖');
    //计数单位
    $arr_char_digit = array('0零','十拾','百佰','千仟','万','亿');

    //提取数字
    $temp_arr_match_num = "1一壹2二贰两3三叁4四肆5五伍6六陆7七柒8八捌9九玖0零十拾百佰千仟万亿";
    $temp_len = iconv_strlen($str,'utf-8');
    $temp_ans = "";
    $temp_start = false;
    for($i = 0;$i < $temp_len;$i++)
    {
        $temp_char = mb_substr($str,$i,1,'utf-8');
        if((stripos($temp_arr_match_num, $temp_char) > -1))
        {
            $temp_start = true;
            $temp_ans .= $temp_char;
        }
        else if(!$solve_discrete && $temp_start)
        {
            //$solve_sp:false,只处理连续的数
            break;
        }
    }

    $str = $temp_ans;
    $str_len = iconv_strlen($str,'utf-8');

    //结构编码
    $objs_code = "";
    //结构
    $objs = [];
    for($i = 0;$i < $str_len;$i++)
    {
        //当前字符
        $temp_char = mb_substr($str,$i,1,'utf-8');

        //是否数字
        $isNum = false;
        for($index = 0 ; $index < count($arr_char_unit);$index++)
        {
            if((stripos($arr_char_unit[$index], $temp_char) > -1))
            {
                $objs_code .='u';
                $objs[] = [
                    'type' => 'unit',
                    'value' => $index+1
                ];
                $isNum = true;
                break;
            }
        }

        if($isNum){continue;}

        //是否计数单位
        for($index = 0 ; $index < count($arr_char_digit);$index++)
        {
            if((stripos($arr_char_digit[$index], $temp_char) > -1))
            {
                if(0 != $index)
                {
                    $objs_code .='d';
                }
                $objs[] = [
                    'type' => 'digit',
                    'value' => (0 == $index?0:pow(10,$index))
                ];
                break;
            }
        }
    }

    $ans = "";
    //根据结构编码选择组合方式
    if(preg_match("/^u+?$/u", $objs_code, $ms))
    {
        foreach ($objs as $obj)
        {
            $ans .= $obj['value'];
        }
    }
    else if(preg_match("/^[ud]{0,1}(ud|du)*[ud]*$/u", $objs_code, $ms))
    {
        $unit = -1;
        $temp_ans = 0;
        foreach ($objs as $obj)
        {
            if('unit' == $obj['type'])
            {
                if(-1 < $unit)
                {
                    $temp_ans += $unit;
                    $ans .= $temp_ans;
                    $temp_ans = 0;
                }
                $unit = $obj['value'];
            }
            else if('digit' == $obj['type'])
            {
                if(0 < $obj['value'])
                {
                    if(-1 < $unit)
                    {
                        $temp_ans += $unit*$obj['value'];
                    }
                    else
                    {
                        $temp_ans += $obj['value'];
                    }
                }
                $unit = -1;
            }
        }

        if(-1 < $unit)
        {
            $temp_ans += $unit;
            $ans .= $temp_ans;
        }
        if("" == $ans)
        {
            $ans .= $temp_ans;
        }
    }
    return $ans;
}

// 测试数据
echo "通过的正常格式\n";
$temp = '从七开始';
echo charN2intN($temp)."\t\t\t".$temp;
echo "\n";
$temp = '从十开始';
echo charN2intN($temp)."\t\t\t".$temp;
echo "\n";
$temp = '从十七开始';
echo charN2intN($temp)."\t\t\t".$temp;
echo "\n";
$temp = '从七十开始';
echo charN2intN($temp)."\t\t\t".$temp;
echo "\n";
$temp = '从七十七开始';
echo charN2intN($temp)."\t\t\t".$temp;
echo "\n";
$temp = '从七佰零七开始';
echo charN2intN($temp)."\t\t\t".$temp;
echo "\n";
$temp = '从七佰一十七开始';
echo charN2intN($temp)."\t\t\t".$temp;
echo "\n";
$temp = '从七佰七十七开始';
echo charN2intN($temp)."\t\t\t".$temp;
echo "\n";
$temp = '从七千零七开始';
echo charN2intN($temp)."\t\t".$temp;
echo "\n";
$temp = '从七千零七十七开始';
echo charN2intN($temp)."\t\t".$temp;
echo "\n";
$temp = '从七千七佰零七开始';
echo charN2intN($temp)."\t\t".$temp;
echo "\n";
$temp = '从七千七佰七十七开始';
echo charN2intN($temp)."\t\t".$temp;
echo "\n";
echo "\n";

echo "通过的非正常格式\n";
$temp = '从七千零七十七开始到12';
echo charN2intN($temp)."\t\t".$temp;
echo "\n";
$temp = '从一千四百八十七开始到12';
echo charN2intN($temp)."\t\t".$temp;
echo "\n";
$temp = '从五七开始到七零七零零';
echo charN2intN($temp)."\t\t".$temp;
echo "\n";
$temp = '从五七开始到七零七00';
echo charN2intN($temp)."\t\t".$temp;
echo "\n";
$temp = '从203开始到七零七';
echo charN2intN($temp)."\t\t".$temp;
echo "\n";
$temp = '从203开始到707';
echo charN2intN($temp)."\t\t".$temp;
echo "\n";
$temp = '从一千四百八十开始到12';
echo charN2intN($temp)."\t\t".$temp;
echo "\n";
$temp = '从七千零七十七开始到七千零七十七';
echo charN2intN($temp)."\t".$temp;
echo "\n";
echo "\n";

echo "solve_discrete:false,只处理连续的数\n";
echo "\n";
echo "通过的正常格式\n";
$temp = '从七开始';
echo charN2intN($temp,false)."\t\t\t".$temp;
echo "\n";
$temp = '从十开始';
echo charN2intN($temp,false)."\t\t\t".$temp;
echo "\n";
$temp = '从十七开始';
echo charN2intN($temp,false)."\t\t\t".$temp;
echo "\n";
$temp = '从七十开始';
echo charN2intN($temp,false)."\t\t\t".$temp;
echo "\n";
$temp = '从七十七开始';
echo charN2intN($temp,false)."\t\t\t".$temp;
echo "\n";
$temp = '从七佰零七开始';
echo charN2intN($temp,false)."\t\t\t".$temp;
echo "\n";
$temp = '从七佰一十七开始';
echo charN2intN($temp,false)."\t\t\t".$temp;
echo "\n";
$temp = '从七佰七十七开始';
echo charN2intN($temp,false)."\t\t\t".$temp;
echo "\n";
$temp = '从七千零七开始';
echo charN2intN($temp,false)."\t\t".$temp;
echo "\n";
$temp = '从七千零七十七开始';
echo charN2intN($temp,false)."\t\t".$temp;
echo "\n";
$temp = '从七千七佰零七开始';
echo charN2intN($temp,false)."\t\t".$temp;
echo "\n";
$temp = '从七千七佰七十七开始';
echo charN2intN($temp,false)."\t\t".$temp;
echo "\n";
echo "\n";

echo "通过的非正常格式\n";
$temp = '从七千零七十七开始到12';
echo charN2intN($temp,false)."\t\t".$temp;
echo "\n";
$temp = '从一千四百八十七开始到12';
echo charN2intN($temp,false)."\t\t".$temp;
echo "\n";
$temp = '从五七开始';
echo charN2intN($temp,false)."\t\t\t".$temp;
echo "\n";
$temp = '从五七开始到七零七零零';
echo charN2intN($temp,false)."\t\t\t".$temp;
echo "\n";
$temp = '从五七开始到七零七00';
echo charN2intN($temp,false)."\t\t\t".$temp;
echo "\n";
$temp = '从203开始到七零七';
echo charN2intN($temp,false)."\t\t\t".$temp;
echo "\n";
$temp = '从203开始到707';
echo charN2intN($temp,false)."\t\t\t".$temp;
echo "\n";
echo "\n";

测试结果

结果参数
正常格式
7从七开始
10从十开始
17从十七开始
70从七十开始
77从七十七开始
707从七佰零七开始
717从七佰一十七开始
777从七佰七十七开始
7007从七千零七开始
7077从七千零七十七开始
7707从七千七佰零七开始
7777从七千七佰七十七开始
非正常格式
707712从七千零七十七开始到12
148712从一千四百八十七开始到12
5770700从五七开始到七零七零零
5770700从五七开始到七零七00
203707从203开始到七零七
203707从203开始到707
70777077从七千零七十七开始到七千零七十七
不能通过的非正常格式
14812从一千四百八十开始到12
$solve_sp:false,只处理连续的数
通过的正常格式
7从七开始
10从十开始
17从十七开始
70从七十开始
77从七十七开始
707从七佰零七开始
717从七佰一十七开始
777从七佰七十七开始
7007从七千零七开始
7077从七千零七十七开始
7707从七千七佰零七开始
7777从七千七佰七十七开始
通过的非正常格式
7077从七千零七十七开始到12
1487从一千四百八十七开始到12
57从五七开始
57从五七开始到七零七零零
57从五七开始到七零七00
203从203开始到七零七
203从203开始到707
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值