duansao20000508 2013-08-10 18:59
浏览 120
已采纳

的file_get_contents(); 不再使用html表?

I was using a code that always worked for me when i need to get external html content but, since yesterday, the code no longer works.

Here´s the code setup

ini_set('display_errors', 1);
$url ='https://www3.bcb.gov.br/ptax_internet/consultarTodasAsMoedas.do?method=consultaTodasMoedas';
$site = file_get_contents($url);

This link is a Currency Converter from Brazil Central Bank and have daily updates (Business Days).

I Need to get the USD and EUR values from this page, when i try to get values inside the Table it return empty but when i get values inside a div everything work as expected.

If i go to the page Source Code i can see:

<tr class="fundoPadraoBClaro2">
     <td align="CENTER">220</td>
     <td align="CENTER">A</td>
     <td align="CENTER">USD</td>
     <td align="right">2,2743</td>
     <td align="right">2,2748</td>
     <td align="right">1,0000</td>
     <td align="right">1,0000</td>
</tr>

To get the Dollar Value i use:

$data1 = explode('<td align="CENTER">USD</td><td align="right">', $site);
$data2 = explode('</td>',$data1[1]);
$usd = $data2[0];

But don´t work, this returns empty values.

But if i use the same code to get contents inside a DIV everything Works ok.

Source Code:

<div align="center">
    <strong>Cotações de todas as moedas no dia 09/08/2013 às 13:00 (horário de Brasília), conforme fechamento PTAX.</strong>
</div>

PHP Code:

$data1 = explode('todas as moedas no dia', $site);
$data2 = explode('conforme fechamento PTAX.',$data1[1]);
$result = $data2[0];

I get what i expected - "09/08/2013 às 13:00 (horário de Brasília)"

Can anyone tell me what i´m missing ?

  • 写回答

2条回答 默认 最新

  • douwen9540 2013-08-10 19:18
    关注

    The problem is that you're not taking the whitespace between <td align="CENTER">USD</td> and <td align="right"> into account.

    This is a perfect situation where you would use regular expressions instead of string manipulations:

    Code

    $matches = array();
    if (preg_match("/USD<\\/td>\\s+<td[^>]*>(?<usd_value>[0-9,.-]*)<\\/td>/i", $site, $matches))
        echo $matches["usd_value"];
    

    The regex searches USD followed by </td>, followed by whitespace, then <td...> tag. Everything between that <td...> and next </td> that looks like a number is captured into the group with name usd_value

    Output

    2,2743
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向