简体   繁体   中英

parse currency name to integer values in PHP

I have a currency value as follows say,

$6.041 billion USD (2006) or US$6.041 billion (2009)[1] or €6.041 billion (2010)[1] .

I want to parse the currency value in such a way that I want to store it in three different variables ie $number, $currency, $year (ie) $number = 6,041,000,000 and $currency = "euro" and $year = 2010 .

The problem is that the string might have or $ or USD . But I need to parse them accordingly.

Also I might end up having million also. According to that succeeding zeros should vary. Also I might or might not have decimal points in the currency. ie 6.041 billion or 6 billion .

How to handle all the cases and store the result in the three variables I need?

similarly how to handle £(67.1) million (2011)[1] HK $ 648 million (2006) 22,440,000, 1,325.26 crore (US$241.2 million) [4]. ?

I am thinking of a brute force solution to handle each case one by one. But that is not a apt one.

Is there any simplest way to do this?

Any help would be appreciated?

You could try running regular expressions something like this (untested):

if(preg_match('/([$€])([\d\.]+)\s?([\w]+)[^\(]*\((\d+)\)/',$value,$matches)){
    switch($matches[1]){
        case '$':
            $currency = 'dollar';
            break;
        case '€':
            $currency = 'euro';
            break;
        // and more for more currencies
    }
    $number = $matches[2];
    switch($matches[3]){
        case 'billion':
            $number = intval($number*1000000000);
            break;
        case 'million':
            $number = intval($number*1000000);
            break;
        // and more for more multipliers
    }
    $year = $matches[4];
}

Remember to add all the possible currency symbols you need to support in the first pair of square brackets in the regular expression [$€] .

Untested and I am sure there are more elegant ways to do things, but this should work:

<?php

echo parseCurrency('$6.041 billion USD (2006)');


function parseCurrency($input){
 if(strpos($input, 'USD') || strpos($input, '$')){
     $currency = 'USD';
     $floatVal = (float) get_string($input, '$', ' ');
 }
 elseif(strpos($input, '€')){
     $currency = 'EUR';
     $floatVal = (float) get_string($input, '€', ' ');
 }
 else{
     $currency = 'undefined';
     die();
 }

 if(strpos($input, 'billion'){
    $number = $floatVal * 1000000000;
 } 
 elseif(strpos($input, 'million'){
    $number = $floatVal * 1000000;
 }
 else{
    $number = 'undefined';
    die();
 }
 if (preg_match('/\\([12][0-9]{3}\\)/', $input, $years)){
    $year = $years[0];
 }
 else{
    $year = 'undefined';
    die();
 }
 return $number . ', ' . $currency . ', ' . $year;
}

//used to find million or billion
function get_string($string, $start, $end){
 $string = " ".$string;
 $pos = strpos($string,$start);
 if ($pos == 0) return "";
 $pos += strlen($start);
 $len = strpos($string,$end,$pos) - $pos;
 return substr($string,$pos,$len);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM