Topic: Exchange Rates - ECB rates parsed as strings

I have been doing some work with the Exchange Rates function and noticed that the ECB rates are in XML format yet are being parsed using string functions and regular expressions. While this works, it is not optimal.

I have rewritten this using SimpleXMLElements to parse it.

gl/includes/db/gl_db_rates.inc
@@ -198,21 +205,32 @@ function get_extern_rate($curr_b, $provider = 'ECB', $date)
     $val = '';
     if ($provider == 'ECB')
     {
-        $contents = str_replace ("<Cube currency='USD'", " <Cube currency='EUR' rate='1'/> <Cube currency='USD'", $contents);
-        $from_mask = "|<Cube\s*currency=\'" . $curr_a . "\'\s*rate=\'([\d.,]*)\'\s*/>|i";
-        preg_match ( $from_mask, $contents, $out );
-        $val_a = isset($out[1]) ? $out[1] : 0;
-        $val_a = str_replace ( ',', '', $val_a );
-        $to_mask = "|<Cube\s*currency=\'" . $curr_b . "\'\s*rate=\'([\d.,]*)\'\s*/>|i";
-        preg_match ( $to_mask, $contents, $out );
-        $val_b = isset($out[1]) ? $out[1] : 0;
-        $val_b = str_replace ( ',', '', $val_b );
-        if ($val_b) 
-        {
+        $data = new SimpleXMLElement($contents);
+
+        $val_a = 0;
+        $val_b = 0;
+
+        foreach ($data->Cube->Cube->Cube as $rate) {
+            if ($curr_a == 'EUR') {
+                $val_a = 1;
+            }
+            elseif ($curr_a == $rate["currency"] )
+            {
+                $val_a = (float) $rate["rate"];
+            }
+
+            if ($curr_b == 'EUR') {
+                $val_b = 1;
+            }
+            elseif ($curr_b == $rate["currency"] )
+            {
+                $val_b = (float) $rate["rate"];
+            }
+        }
+
+        if ($val_b) {
             $val = $val_a / $val_b;
-        } 
-        else 
-        {
+        } else {
             $val = 0;
         }
     }

Re: Exchange Rates - ECB rates parsed as strings

@joe: ?