search
Homephp教程PHP源码中文数字转阿拉伯数字

跳至 [1] [全屏预览]
function strToNum($str){
    $arr1=['零'=>'', '一'=>1, '二'=>2, '三'=>3, '四'=>4, '五'=>5, '六'=>6, '七'=>7, '八'=>8, '九'=>9];
    $arr2=['亿'=>100000000,'千万'=>10000000,'百万'=>1000000,'十万'=>100000,'万'=>10000,'千'=>1000,'百'=>100,'十'=>10];
    preg_match_all('/(零|一|二|三|四|五|六|七|八|九|十|百|千|万|亿)+/i',$str,$result);
    if(empty($result[0][0])) return $str;
    else $tmp=$result[0][0];
    $tmp=str_replace(array_keys($arr1), array_values($arr1), $tmp);
    foreach ($arr2 as $k => $v) {
    	if(strlen($tmp)==1) $tmpArr[1]=$tmp;
    	else if(strpos($tmp, $k)!==false){
    		$tmpArr[$v]=getFromStr('',$k,$tmp);
    		$tmp=getFromStr($k,'',$tmp);
    		if(strlen($tmp)==1) $tmpArr[1]=$tmp;
    	}
    }
   	if(is_array($tmpArr)){
   		$num=0;
   		foreach ($tmpArr as $k => $v) {
   			if(empty($v)) $v=1;
   			$num+=$k*$v;
   		}
   	}

    if(!empty($num)) return str_replace($result[0][0], $num, $str);else return $str;
}

function getFromStr($start,$end,$str)
{
    if(!empty($start)) $str=substr($str,strpos($str,$start)+strlen($start) ,strlen($str)-strlen($start)-strpos($str,$start));
    if(!empty($end)) $str=substr($str,0,strpos($str,$end));
    return $str;
}
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)