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!
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.
//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
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:
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.
//$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);
^ 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:
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.