在 PHP 中解析时取消货币格式
有没有办法获取像这样的字符串的浮点值:75,25 €
,而不是parsefloat(str_replace(',', '.', $var))?
我希望这取决于当前的站点语言,有时逗号可以用点代替。
Is there a way to get the float value of a string like this: 75,25 €
, other than parsefloat(str_replace(',', '.', $var))
?
I want this to be dependent on the current site language, and sometimes the comma could be replaced by dot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是一个稍微复杂/缓慢的解决方案,但适用于所有区域设置。 @rlenom 的解决方案仅适用于点作为小数分隔符,而某些语言环境(例如西班牙语)使用逗号作为小数分隔符。
测试:
注意事项:
如果小数部分超过两位数则失败。
这是该库的一个实现:
https://github.com/mcuadros/currency-detector
This is a bit more complex/ slow solution, but works with all locales. @rlenom's solution work only with dots as decimal separator, and some locales, like Spanish, use the comma as decimal separator.
Tests:
Caveats:
Fails if the decimal part have more than two digits.
This is an implementation from this library:
https://github.com/mcuadros/currency-detector
您可以使用
NumberFormatter::parseCurrency
- 解析货币数字手册中的示例:
给出:
float(75.25)
请注意 国际扩展 默认情况下不启用。请参阅安装说明。
You can use
NumberFormatter::parseCurrency
- Parse a currency numberExample from Manual:
gives:
float(75.25)
Note that the intl extension is not enabled by default. Please refer to the Installation Instructions.
使用 ereg_replace
输出
更新
输出
如果将其转换为整数
输出
那么你就得到了它。单行,一次替换。你可以走了。
use ereg_replace
outputs
Update
Outputs
and if you cast it as an integer
outputs
So there you have it. single line, one replace. you're good to go.
您需要从字符串中删除货币符号。 PHP 的
intval
在找到的第一个非数字字符处停止。不过,如果您的值类似于
$100.25
,您可能需要使用floatval
来代替。You're gonna need to remove the currency symbol from the string. PHP's
intval
stops at the 1st non-numeric character it finds.Though if you have a value like
$100.25
, you might wanna usefloatval
instead.PHP 有
intval
(这里是文档),这是(据我所知告诉)与 JavaScript 的parseInt
完全相同。然而,就其价值而言,我认为这两个功能都无法帮助您完成您想要做的事情。因为第一个字符是非数字,所以两者都吓坏了(PHP 会给你 0,JS 会给你
NaN
)。因此,无论哪种语言,您都必须进行一些字符串/正则表达式解析。PHP has
intval
(here are the docs), which is (as far as I can tell) exactly the same as JavaScript'sparseInt
.However, for what's worth, I don't think either function will help you with what you're trying to do. Because the first character is non-numeric, both freak out (PHP will give you 0, JS will give you
NaN
). So in either language, you're going to have to do some string/regex parsing.我是新手,所以下面的方法可能有一个明显的(对其他人,而不是我)缺点,但我想无论如何都会分享它。我有兴趣知道它比使用 preg_replace 更快还是更慢,但没有进行任何速度测试。
$dirtyString 可以是一个数组,所以:
I'm an newbie, so there's probably an obvious (to others, not me) downside to the approach below, but thought I would share it anyway. I'd be interested to know whether it's faster or slower than using preg_replace, but didn't do any speed testing.
$dirtyString can be an array, so:
铸造是你的朋友:
更新基于op:
尝试这样的事情:
演示:http://codepad.org/QyrfS7WE
Casting is your friend:
Update based on op:
Try something like this:
Demo: http://codepad.org/QyrfS7WE
我遇到了类似的问题,我没有收到货币符号,只收到字符串(即:1,234,567.89 或 1.234.567,89)。
这帮助我将这两种情况标准化为浮点数:
但戈登的答案更加清晰。
I had a similar problem where I didn't receive the currency symbol, just the strings (ie: 1,234,567.89 or 1.234.567,89).
This helped me normalize both cases into floats:
But Gordon's answer is much cleaner.
filter_var 比正则表达式解决方案快得多:
1345.00
让它变得更复杂是没有意义的。
filter_var is much faster than regex solutions:
1345.00
No sense making it more complicated than it is.