Jump to content

[SOLVED] file_get_contents with preg_match


hackerkts

Recommended Posts

I'm using the script posted by HuggieBear at http://www.phpfreaks.com/forums/index.php/topic,124091.msg513961.html#msg513961

 

I'm trying to modify it and make it works for all the currencies show on that page, currently I having problem to get the exchange rates for United States Dollar and Euro working but I couldn't.

 

<?php
$url = "http://newsvote.bbc.co.uk/1/shared/fds/hi/business/market_data/currency/11/12/default.stm";
$raw_data = file_get_contents($url);
preg_match('/United States Dollar.*?right">([^<]+)/is', $raw_data, $USD);
preg_match('/Euro.*?right">([^<]+)/is', $raw_data, $EUR);
$ex_rate_USD = number_format($USD[1], 2, '.', '');
$ex_rate_EUR = number_format($EUR[1], 2, '.', '');
?>

 

I think it's because the pattern for USD and EUR is different from the rest, by the way. Can someone explain to me the pattern used here?

/United States Dollar.*?right">([^<]+)/is

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/48457-solved-file_get_contents-with-preg_match/
Share on other sites

<pre>
<?php
$url = "http://newsvote.bbc.co.uk/1/shared/fds/hi/business/market_data/currency/11/12/default.stm";
$raw_data = file_get_contents($url);
preg_match_all('#(?<=class="statsclick">)(.+?)</A></td>\s+<td[^>]+>(.+?)</td>#', $raw_data, $matches, PREG_SET_ORDER);
// Create associative array.
$rates = array();
foreach ($matches as $match) {
	$rates[$match[1]] = $match[2];
}
unset($matches);
print_r($rates);
?>
</pre>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.