Reputation: 1019
I'm trying to get some exchange rates from another website, I'm logging in and grabbing all the data with file_get_contents, this is what I use:
<?php
$username = '[email protected]';
$password = 'mypassword';
$url = 'http://website-i-get-content-from.com';
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode("$username:$password")
)
));
$data = file_get_contents($url, false, $context)
?>
Now I only need some parts of that website: the exchange rates for EUR CHF and GBP, in the source code it looks like this:
<tr><td>EUR</td><td align=right>USD 0.599</td><td align=right>USD 0.599</td></tr>
<tr><td>CHF</td><td align=right>USD 0.470</td><td align=right>USD 0.470</td></tr>
<tr><td>GBP</td><td align=right>USD 0.675</td><td align=right>USD 0.675</td></tr>
So 0.599, 0.470 and 0.675 are the numbers I need at this time. They do change obviously.
How do I put them into variables ?
Upvotes: 0
Views: 2459
Reputation: 36733
Sounds like you need a parser. I've used simpledom parser in the past. I found it quite straightforward.
include("simplehtmldom/simple_html_dom.php");
$data="<html>
<body>
<table class=\"foo\">
<tr><td>EUR</td><td align=right>USD 0.599</td><td align=right>USD
0.599</td></tr>
<tr><td>CHF</td><td align=right>USD 0.470</td><td align=right>USD
0.470</td></tr>
<tr><td>GBP</td><td align=right>USD 0.675</td><td align=right>USD
0.675</td></tr>
</table>
</body>
</html>";
$html = new simple_html_dom();
$html->load($data);
foreach($html->find('table.foo tr') as $row) {
$cells = $row->find('td');
if (count($cells) >= 3) {
$abbr=$cells[0]->innertext; // EUR, CHF etc
$value1=$cells[1]->innertext; // USD 0.599 etc
$value2=$cells[2]->innertext; // USD 0.599 etc
echo "$abbr $value1 $value2\n";
}
}
Upvotes: 3
Reputation: 9487
A regular expression could do.
preg_match_all("'(EUR|CHF|GBP)(?=<).+USD(?<=>)\s+([\d.]+)(?=<)'", file_get_contents('...'), $matches));
I didn't test it though.
I know it's bad bla bla to parse HTML but it's not really parsing here.
Upvotes: 0