Extracting rate value for a given currency

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Extracting rate value for a given currency

Post by Rovas »

I hit a wall with a trivial problem: extract from a given file the currency and it' s exchange rate. The file is located here. I tried at first using SimpleXML and XPath, then I tried using regular expressions. I checked the regular expression in a couple of regex validators and for some I got the desired result while for others (based on PHP) I haven' t.

Code: Select all

 
  //second method using regex
   //cache_file is the file on the disk where I copy the content of ECB file so that I don' t make a lot of requests for it.
  $c=fread($cache_file,filesize($cache_exchange));
  $name="USD";
  $exp='/ currency="' .$name .' rate="[0-9]{1,5}.[0-9]{2,9}" /';
  $b=preg_match($exp,$c,$a);
  print_r($a);
  //it returns Array() 
 
Another question: Why in PHP' s XPath doesn' t recognize

Code: Select all

 
//Cube[@currency='USD'] | //@currency 
 

for "complicated" files?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Extracting rate value for a given currency

Post by Mark Baker »

Rovas wrote:Another question: Why in PHP' s XPath doesn' t recognize

Code: Select all

 
//Cube[@currency='USD'] | //@currency 
 

for "complicated" files?
Possibly because Cube is ambiguous:

Code: Select all

 
    <Cube>
        <Cube time="2008-11-10">
            <Cube currency="USD" rate="1.2891" /> 
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Extracting rate value for a given currency

Post by VladSun »

I've never used PHP SimpleXML Parser but this looks like it works ;)

Code: Select all

$xmlstr = file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
$xml = new SimpleXMLElement($xmlstr);
 
foreach ($xml->Cube[0]->Cube[0]->Cube as $currency)
    echo $currency->attributes()->currency." ".$currency->attributes()->rate."\n";
There are 10 types of people in this world, those who understand binary and those who don't
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Re: Extracting rate value for a given currency

Post by Rovas »

VladSunI thank for your post but the code you put outputs the whole file. I don' t need that, I need only the rate for a specific currency , for example the user selects to transform euro into his local currency yen so the script has to return 127.89 for further calculations. I already tried to use a similar script and array_search to find the desired currency unfortunetly array_search doesn' t work with associative arrays.
Can you tell what' s wrong with the regular expression:

Code: Select all

 
$exp='/ currency="' .$name .' rate="[0-9]{1,5}.[0-9]{2,9}" /';
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Extracting rate value for a given currency

Post by VladSun »

A few things are wrong ;)
1. You need to specify the part you want to match by using ().
2. The dot (.) is a special symbol - it means any symbol except new line chars, so you need to escape it.
E.g.

Code: Select all

.. rate="([0-9]{1,5}\.[0-9]{2,9})" ...
3. You didn't close the quotes after $name in your regexp
4. You need to match against single quotes, not double ones ;)
There are 10 types of people in this world, those who understand binary and those who don't
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Re: Extracting rate value for a given currency

Post by Rovas »

After I fix the errors you pointed out but it' s still doesn' t work. Maybe I' m too frustrated by this and I can' t figure out what I do wrong.

Code: Select all

 
        //$c = content of http://www.ecb.europa.eu/stats/eurofxre ... -daily.xml
    $exp='/ currency=\"' .$name .'\" rate=\"([0-9]{1,5}\.[0-9]{2,9})\" /';
        //$a an array to put the result
    preg_match($exp,$c,$a);
    print_r($a);
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Extracting rate value for a given currency

Post by VladSun »

VladSun wrote:4. You need to match against single quotes, not double ones ;)
It's

Code: Select all

<Cube currency='USD' rate='1.2891' />
not

Code: Select all

<Cube currency="USD" rate="1.2891" />
There are 10 types of people in this world, those who understand binary and those who don't
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Re: Extracting rate value for a given currency

Post by Rovas »

^ It doesn' t matter if it single or double quotes it still doesn' t work. I tried other regular expressions but when I use the two xml attributes currency and rate it doesn' t work anymore.
I don' t understand what' s wrong with:

Code: Select all

$exp="/ (currency='[" .$name."]{3}') (rate='[0-9]{1,5}\.[0-9]{2,9}') /"
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Extracting rate value for a given currency

Post by VladSun »

:? :? :?

http://www.weitz.de/regex-coach/

Code: Select all

$xmlstr = file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
 
$name = "USD";
preg_match("/currency='".$name."' rate='([0-9]{1,5}\.[0-9]{2,9})'/", $xmlstr, $a);
print_r($a);
 
There are 10 types of people in this world, those who understand binary and those who don't
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Re: Extracting rate value for a given currency

Post by Rovas »

This is the expression that I come up using regex checker 1 (only IE) and regex checker 2

Code: Select all

(currency=\"[USD]{3}\" (rate=\"[0-9]{1,5}\.[0-9]{2,9}))
It works also in the Regex Coach but not on my server indifferent of what function I use: ereg, preg_match, eregi, etc.
EDIT: Sorry, I forgot to thank you for the solution. Thank you very much but :( I would like it to do figure it out myself.
Post Reply