从字符串中分割货币和金额

发布于 2024-09-05 08:58:25 字数 637 浏览 6 评论 0原文

我正在导入一个文件,其中包含不同货币符号的金额,

£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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇俏 2024-09-12 08:58:25

您不应使用拆分,而应使用模式匹配来识别所使用的金额和货币。因为在某些区域设置中,货币符号出现在金额之前,而在其他区域设置中,货币符号出现在金额后面。此外,在某些语言环境中,符号和金额由空格分隔。

可以使用以下函数:

   function findAmountAndCurrency($s, &$amount, &$currency){
     $re_amount="/[0-9\.]+/";
     $re_curr="/[£\$€]+/";

     preg_match($re_amount, $s, $matches);
     $amount = floatval($matches[0]);

     preg_match($re_curr, $s, $matches);
     $currency = $matches[0];
   }

这就是它的使用方式:

  function handle($s){
    $currencySymbols = array('£'=>'GBP', '

如果您有 UTF-8 输入,您可能会遇到欧元符号问题。现在无法检查那里的解决方案。也许其他人可以帮忙。

=>'USD','€'=>'EUR'); findAmountAndCurrency($s, $amount, $currency); echo("Amount: " . $amount . "<br/>"); echo("Currency: " . $currency . "<br/>"); echo("Identified Currency: " . $currencySymbols[$currency] . "<br/>"); } handle("£12.10"); handle("3.212 €"); handle("$ 99.99");

如果您有 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:

   function findAmountAndCurrency($s, &$amount, &$currency){
     $re_amount="/[0-9\.]+/";
     $re_curr="/[£\$€]+/";

     preg_match($re_amount, $s, $matches);
     $amount = floatval($matches[0]);

     preg_match($re_curr, $s, $matches);
     $currency = $matches[0];
   }

This is how it would be employed:

  function handle($s){
    $currencySymbols = array('£'=>'GBP', '

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.

=>'USD','€'=>'EUR'); findAmountAndCurrency($s, $amount, $currency); echo("Amount: " . $amount . "<br/>"); echo("Currency: " . $currency . "<br/>"); echo("Identified Currency: " . $currencySymbols[$currency] . "<br/>"); } handle("£12.10"); handle("3.212 €"); handle("$ 99.99");

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文