从字符串中分割货币和金额
我正在导入一个文件,其中包含不同货币符号的金额,
£12.10
$26.13
€12.50
我需要将其导入并将其转换为单一货币。我按如下方式分割字符串
$parts = split(' ', preg_replace("/([0-9])/", ' ${1}', $amount, 1));
无法使 preg_split 与 PREG_SPLIT_DELIM_CAPTURE 一起使用
$parts = preg_split("/\d/", $amount, 2, PREG_SPLIT_DELIM_CAPTURE);
我有一组货币符号到货币代码
$currencySymbols = array('£'=>'GBP', '$'=>'USD','€'=>'EUR')
我需要 1.将字符串拆分为货币符号和值 - 如果有更好的方法那么我正在做的 2. 将货币符号映射到货币代码。无法使用 $currencySymbols[$parts[0]] 进行映射
任何帮助将不胜感激。 (PHP 5.2.6)使用 charset=utf-8
非常感谢
I am importing a file which has amount with different currency signs
£12.10
$26.13
€12.50
I need to import and convert this into single currency. I am splitting the string as follows
$parts = split(' ', preg_replace("/([0-9])/", ' ${1}', $amount, 1));
Couldn't make preg_split work with PREG_SPLIT_DELIM_CAPTURE
$parts = preg_split("/\d/", $amount, 2, PREG_SPLIT_DELIM_CAPTURE);
I have an array of currency sign to currency code
$currencySymbols = array('£'=>'GBP', '
I need to
1. split the string into currency sign and value - if there is a better way then what i am doing
2. map the currency sign to currency code. Not able to map with $currencySymbols[$parts[0]]
Any help will be appreciated. (PHP 5.2.6) using charset=utf-8
Many thanks
=>'USD','€'=>'EUR')
I need to
1. split the string into currency sign and value - if there is a better way then what i am doing
2. map the currency sign to currency code. Not able to map with $currencySymbols[$parts[0]]
Any help will be appreciated. (PHP 5.2.6) using charset=utf-8
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能应该使用 NumberFormatter::parseCurrency 来执行此操作,而不是常规字符串操作。
虽然大多数时候货币符号会出现在数值之前,但有些欧洲国家/地区的货币符号写在最后。
You should probably use the NumberFormatter::parseCurrency to do this instead of regular string manipulation.
Although most of the time the currency symbol will come before the numeric value, there are some European countries where the currency symbol is written last.
您不应使用拆分,而应使用模式匹配来识别所使用的金额和货币。因为在某些区域设置中,货币符号出现在金额之前,而在其他区域设置中,货币符号出现在金额后面。此外,在某些语言环境中,符号和金额由空格分隔。
可以使用以下函数:
这就是它的使用方式:
如果您有 UTF-8 输入,您可能会遇到欧元符号问题。现在无法检查那里的解决方案。也许其他人可以帮忙。
You shouldn't use splitting, but pattern matching to identify the amount and the currency used. Because in some locales, the currency symbol appears before the amount, in others, it appears behind the amount. Also, in some locales, symbol and amount are separated by spaces.
The following function can be used:
This is how it would be employed:
You might get a problem with the EURO sign if you have UTF-8 input. Can't check the solution there right now. Maybe someone else can help out.