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
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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.