<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[FrontAccounting forum — Exchange Rates - ECB rates parsed as strings]]></title>
		<link>https://frontaccounting.com/punbb/viewtopic.php?id=9222</link>
		<atom:link href="https://frontaccounting.com/punbb/extern.php?action=feed&amp;tid=9222&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in Exchange Rates - ECB rates parsed as strings.]]></description>
		<lastBuildDate>Mon, 08 Mar 2021 07:40:19 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: Exchange Rates - ECB rates parsed as strings]]></title>
			<link>https://frontaccounting.com/punbb/viewtopic.php?pid=39237#p39237</link>
			<description><![CDATA[<p>@joe: ?</p>]]></description>
			<author><![CDATA[null@example.com (apmuthu)]]></author>
			<pubDate>Mon, 08 Mar 2021 07:40:19 +0000</pubDate>
			<guid>https://frontaccounting.com/punbb/viewtopic.php?pid=39237#p39237</guid>
		</item>
		<item>
			<title><![CDATA[Exchange Rates - ECB rates parsed as strings]]></title>
			<link>https://frontaccounting.com/punbb/viewtopic.php?pid=39225#p39225</link>
			<description><![CDATA[<p>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.</p><p>I have rewritten this using SimpleXMLElements to parse it. </p><div class="codebox"><pre><code>gl/includes/db/gl_db_rates.inc
@@ -198,21 +205,32 @@ function get_extern_rate($curr_b, $provider = &#039;ECB&#039;, $date)
     $val = &#039;&#039;;
     if ($provider == &#039;ECB&#039;)
     {
-        $contents = str_replace (&quot;&lt;Cube currency=&#039;USD&#039;&quot;, &quot; &lt;Cube currency=&#039;EUR&#039; rate=&#039;1&#039;/&gt; &lt;Cube currency=&#039;USD&#039;&quot;, $contents);
-        $from_mask = &quot;|&lt;Cube\s*currency=\&#039;&quot; . $curr_a . &quot;\&#039;\s*rate=\&#039;([\d.,]*)\&#039;\s*/&gt;|i&quot;;
-        preg_match ( $from_mask, $contents, $out );
-        $val_a = isset($out[1]) ? $out[1] : 0;
-        $val_a = str_replace ( &#039;,&#039;, &#039;&#039;, $val_a );
-        $to_mask = &quot;|&lt;Cube\s*currency=\&#039;&quot; . $curr_b . &quot;\&#039;\s*rate=\&#039;([\d.,]*)\&#039;\s*/&gt;|i&quot;;
-        preg_match ( $to_mask, $contents, $out );
-        $val_b = isset($out[1]) ? $out[1] : 0;
-        $val_b = str_replace ( &#039;,&#039;, &#039;&#039;, $val_b );
-        if ($val_b) 
-        {
+        $data = new SimpleXMLElement($contents);
+
+        $val_a = 0;
+        $val_b = 0;
+
+        foreach ($data-&gt;Cube-&gt;Cube-&gt;Cube as $rate) {
+            if ($curr_a == &#039;EUR&#039;) {
+                $val_a = 1;
+            }
+            elseif ($curr_a == $rate[&quot;currency&quot;] )
+            {
+                $val_a = (float) $rate[&quot;rate&quot;];
+            }
+
+            if ($curr_b == &#039;EUR&#039;) {
+                $val_b = 1;
+            }
+            elseif ($curr_b == $rate[&quot;currency&quot;] )
+            {
+                $val_b = (float) $rate[&quot;rate&quot;];
+            }
+        }
+
+        if ($val_b) {
             $val = $val_a / $val_b;
-        } 
-        else 
-        {
+        } else {
             $val = 0;
         }
     }</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (PaulShipley)]]></author>
			<pubDate>Sun, 07 Mar 2021 01:52:47 +0000</pubDate>
			<guid>https://frontaccounting.com/punbb/viewtopic.php?pid=39225#p39225</guid>
		</item>
	</channel>
</rss>
