Page 1 of 1

Extracting rate value for a given currency

Posted: Mon Nov 10, 2008 8:41 am
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?

Re: Extracting rate value for a given currency

Posted: Mon Nov 10, 2008 8:50 am
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" /> 
 

Re: Extracting rate value for a given currency

Posted: Mon Nov 10, 2008 9:07 am
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";

Re: Extracting rate value for a given currency

Posted: Mon Nov 10, 2008 12:16 pm
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}" /';
 

Re: Extracting rate value for a given currency

Posted: Mon Nov 10, 2008 3:10 pm
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 ;)

Re: Extracting rate value for a given currency

Posted: Tue Nov 11, 2008 1:47 am
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);
 

Re: Extracting rate value for a given currency

Posted: Tue Nov 11, 2008 5:36 am
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" />

Re: Extracting rate value for a given currency

Posted: Tue Nov 11, 2008 7:14 am
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}') /"

Re: Extracting rate value for a given currency

Posted: Tue Nov 11, 2008 7:28 am
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);
 

Re: Extracting rate value for a given currency

Posted: Tue Nov 11, 2008 8:43 am
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.