doujianguang5506 2016-01-21 15:58
浏览 35
已采纳

如何在使用谷歌货币api与PHP时只获得结果值?

I'm using the "new" google currency calculator, and instead of the precedent version, now we have a form, with dropdown menus..etc, what i need is just to get the result value.

This is my simple php function :

    // GOOGLE CURRENCY CONVERTER API
function converter_currency($amount, $from){
    $result = file_get_contents('https://www.google.com/finance/converter?a='.$amount.'&from='.$from.'&to=USD');

    print_r ($result);
    // echo gettype($result); <== said that it's a string
}
  converter_currency(5, 'EUR'); //converts 5euros to USD

That returns a form ..ect with the result : 5 EUR = 5.4050 USD How to get only : 5.4050 ?

Thank you.

  • 写回答

2条回答 默认 最新

  • drlma06060 2016-01-21 16:07
    关注
    function converter_currency($amount, $from){
        $result = file_get_contents('https://www.google.com/finance/converter?a='.$amount.'&from='.$from.'&to=USD');
        preg_match('#\<span class=bld\>(.+?)\<\/span\>#s', $result, $finalData);
        return $finalData[1];
    }
    
    echo converter_currency(5, 'EUR'); // 5.4125 USD
    
    list($amount, $currency) = explode(' ', converter_currency(5, 'EUR'));
    var_dump($amount, $currency); // string(6) "5.4125" string(3) "USD"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法