Possible Duplicate:
PHP: unformat money可能重复:PHP:unformat money
How to get rid of everything that is not a number or dot, replacing ,
with .
using a light regex?
如何摆脱不是数字或点的所有东西,替换,用。使用轻型正则表达式?
Examples:
$50.45 = 50.45
USD 50.45 = 50.45
50,45 = 50.45
USD$ 50.45 = 50.45
4 个解决方案
#1
5
<?php
$money = array(
'$50.45',
'USD 50.45',
'50,45',
'USD$ 50.45'
);
// remove everything except a digit "0-9", a comma ",", and a dot "."
$money = preg_replace('/[^\d,\.]/', '', $money);
// replace the comma with a dot, in the number format ",12" or ",43"
$money = preg_replace('/,(\d{2})$/', '.$1', $money);
print_r($money);
?>
Output:
Array
(
[0] => 50.45
[1] => 50.45
[2] => 50.45
[3] => 50.45
)
#2
1
preg_replace('/.*?(\d+)(?:[.,](\d+))?.*/', '\1.\2', $string);
#3
1
The solution I came up with that works with your examples was:
我提出的与您的示例一起使用的解决方案是:
preg_replace("([^0-9\.])","",str_replace(",",".",$val));
Assuming comma only ever appears when it should be a decimal point, as opposed to being a thousands separator. It replaces those with a decimal place, and then removes all non numeric/decimal characters from the remaining string altogether.
假设逗号只出现在应该是小数点时,而不是千位分隔符。它替换了带小数位的那些,然后从剩余的字符串中删除所有非数字/十进制字符。
Test script:
$inputs = array("\$50.45", "USD 50.45", "50,45", "USD\$ 50.45");
foreach ($inputs as $val) {
$cleanVal = preg_replace("([^0-9\.])","",str_replace(",",".",$val));
echo "GIVEN: <b>".$val."</b> -> CLEAN: <b>".$cleanVal."</b><br/>";
}
Output:
GIVEN: $50.45 -> CLEAN: 50.45
GIVEN:50.45美元 - >清洁:50.45
GIVEN: USD 50.45 -> CLEAN: 50.45
GIVEN:50.45美元 - >清洁:50.45
GIVEN: 50,45 -> CLEAN: 50.45
GIVEN:50,45 - > CLEAN:50.45
GIVEN: USD$ 50.45 -> CLEAN: 50.45
GIVEN:50.45美元 - >清洁:50.45
#4
0
This would work too for the given examples (demo):
这也适用于给定的示例(演示):
$money = array(
'$50.45',
'USD 50.45',
'50,45',
'USD$ 50.45'
);
foreach ($money as $val) {
echo filter_var(
str_replace(',', '.', $val),
FILTER_SANITIZE_NUMBER_FLOAT,
FILTER_FLAG_ALLOW_FRACTION
), PHP_EOL;
}
// gives
// 50.45
// 50.45
// 50.45
// 50.45
This will fail when the string contains fractions and thousand separators. There is a filter flag FILTER_FLAG_ALLOW_THOUSAND
that could potentially handle thousand separators as well. But since the thousand separators can indicate fraction in other locales and you want to take it into account, it wont work in your scenario.
当字符串包含分数和千位分隔符时,这将失败。有一个过滤器标志FILTER_FLAG_ALLOW_THOUSAND也可能处理千位分隔符。但是,由于千位分隔符可以指示其他语言环境中的分数,并且您希望将其考虑在内,因此它不适用于您的方案。
For a more powerful parsing alternative try
对于更强大的解析替代尝试
- numfmt_parse_currency - Parse a string into a double and a currency using the NumberFormatter.
numfmt_parse_currency - 使用NumberFormatter将字符串解析为double和货币。
A simple example can be found in PHP: unformat money. The example code in the accepted answer will only parse $50.45 though. The NumberFormatter can be configured with additional rules, so while I am not 100% certain, I think it should be possible with it.
一个简单的例子可以在PHP中找到:unformat money。接受的答案中的示例代码仅解析50.45美元。 NumberFormatter可以配置其他规则,所以虽然我不是100%肯定,但我认为应该可以使用它。
#1
5
<?php
$money = array(
'$50.45',
'USD 50.45',
'50,45',
'USD$ 50.45'
);
// remove everything except a digit "0-9", a comma ",", and a dot "."
$money = preg_replace('/[^\d,\.]/', '', $money);
// replace the comma with a dot, in the number format ",12" or ",43"
$money = preg_replace('/,(\d{2})$/', '.$1', $money);
print_r($money);
?>
Output:
Array
(
[0] => 50.45
[1] => 50.45
[2] => 50.45
[3] => 50.45
)
#2
1
preg_replace('/.*?(\d+)(?:[.,](\d+))?.*/', '\1.\2', $string);
#3
1
The solution I came up with that works with your examples was:
我提出的与您的示例一起使用的解决方案是:
preg_replace("([^0-9\.])","",str_replace(",",".",$val));
Assuming comma only ever appears when it should be a decimal point, as opposed to being a thousands separator. It replaces those with a decimal place, and then removes all non numeric/decimal characters from the remaining string altogether.
假设逗号只出现在应该是小数点时,而不是千位分隔符。它替换了带小数位的那些,然后从剩余的字符串中删除所有非数字/十进制字符。
Test script:
$inputs = array("\$50.45", "USD 50.45", "50,45", "USD\$ 50.45");
foreach ($inputs as $val) {
$cleanVal = preg_replace("([^0-9\.])","",str_replace(",",".",$val));
echo "GIVEN: <b>".$val."</b> -> CLEAN: <b>".$cleanVal."</b><br/>";
}
Output:
GIVEN: $50.45 -> CLEAN: 50.45
GIVEN:50.45美元 - >清洁:50.45
GIVEN: USD 50.45 -> CLEAN: 50.45
GIVEN:50.45美元 - >清洁:50.45
GIVEN: 50,45 -> CLEAN: 50.45
GIVEN:50,45 - > CLEAN:50.45
GIVEN: USD$ 50.45 -> CLEAN: 50.45
GIVEN:50.45美元 - >清洁:50.45
#4
0
This would work too for the given examples (demo):
这也适用于给定的示例(演示):
$money = array(
'$50.45',
'USD 50.45',
'50,45',
'USD$ 50.45'
);
foreach ($money as $val) {
echo filter_var(
str_replace(',', '.', $val),
FILTER_SANITIZE_NUMBER_FLOAT,
FILTER_FLAG_ALLOW_FRACTION
), PHP_EOL;
}
// gives
// 50.45
// 50.45
// 50.45
// 50.45
This will fail when the string contains fractions and thousand separators. There is a filter flag FILTER_FLAG_ALLOW_THOUSAND
that could potentially handle thousand separators as well. But since the thousand separators can indicate fraction in other locales and you want to take it into account, it wont work in your scenario.
当字符串包含分数和千位分隔符时,这将失败。有一个过滤器标志FILTER_FLAG_ALLOW_THOUSAND也可能处理千位分隔符。但是,由于千位分隔符可以指示其他语言环境中的分数,并且您希望将其考虑在内,因此它不适用于您的方案。
For a more powerful parsing alternative try
对于更强大的解析替代尝试
- numfmt_parse_currency - Parse a string into a double and a currency using the NumberFormatter.
numfmt_parse_currency - 使用NumberFormatter将字符串解析为double和货币。
A simple example can be found in PHP: unformat money. The example code in the accepted answer will only parse $50.45 though. The NumberFormatter can be configured with additional rules, so while I am not 100% certain, I think it should be possible with it.
一个简单的例子可以在PHP中找到:unformat money。接受的答案中的示例代码仅解析50.45美元。 NumberFormatter可以配置其他规则,所以虽然我不是100%肯定,但我认为应该可以使用它。