我目前正在开发一个网站,需要转换后面带有字母的金额,例如:
100M = 100.000.000
我已经创建了一种以另一种方式执行此操作的方法,来自:
100.000.000 = 100M
这是我当前的功能:
function Convert($Input){
if($Input<=1000){
$AmountCode = "GP";
$Amount = $Input;
}
else if($Input>=1000000){
$AmountCode = "M";
$Amount = floatval($Input / 1000000);
}
else if($Input>=1000){
$AmountCode = "K";
$Amount = $Input / 1000;
}
$Array = array(
'Amount' => $Amount,
'Code' => $AmountCode
);
$Result = json_encode($Array);
return json_decode($Result);
}
现在我需要一些可以过滤掉这个的东西:
100GP = 100
100K = 100.000
100M = 100.000.000
我一直在寻找一些东西,并尝试使用 explode();
和其他函数,但它并不像我想要的那样工作..
有谁可以帮我吗?
最佳答案
<?php
/**
* @param string $input
* @return integer
*/
function revert($input) {
if (!is_string($input) || strlen(trim($input)) == 0) {
throw new InvalidArgumentException('parameter input must be a string');
}
$amountCode = array ('GP' => '',
'K' => '000',
'M' => '000000');
$keys = implode('|', array_keys($amountCode));
$pattern = '#[0-9]+(' . $keys .'){1,1}$#';
$matches = array();
if (preg_match($pattern, $input, $matches)) {
return $matches[1];
}
else {
throw new Exception('can not revert this input: ' . $input);
}
}
关于php - 转换输入为 "100K"、 "100M"等的金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32623315/