Skip to content

Instantly share code, notes, and snippets.

@fabiocerqueira
Created January 31, 2012 17:17
Show Gist options
  • Save fabiocerqueira/1711672 to your computer and use it in GitHub Desktop.
Save fabiocerqueira/1711672 to your computer and use it in GitHub Desktop.
Recuperar o dólar turismo e comercial do site: http://www.valor.com.br/valor-data
<?php
function getDolar() {
function normalize($dolar_string) {
preg_match('/<strong>(.*?)<\/strong>/', $dolar_string, $matches);
preg_match_all('/<span.*?>(.*?)<\/span>/', $dolar_string, $out);
preg_match_all('/<span.*? class="variation (.*?)">/', $dolar_string, $style);
$kind_dolar = trim(str_replace(array('.', 'DÓLAR'), array('',''), $matches[1]));
$ret = array(
$kind_dolar => array('value' => $out[1][0],
'variation' => $out[1][1],
'class' => $style[1][0])
);
return $ret;
}
$source = file_get_contents('http://www.valor.com.br/valor-data');
$source = str_replace("\n", "", $source);
preg_match_all('/<div class="item">(.*?)<\/div>/',
$source,
$out, PREG_PATTERN_ORDER);
$dolar_com = $out[0][0];
$dolar_tur = $out[0][1];
$dolar_com = normalize($dolar_com);
$dolar_tur = normalize($dolar_tur);
$dolar = array_merge($dolar_com, $dolar_tur);
return $dolar;
}
$dolar = getDolar();
print_r($dolar);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment