-1

我必须将价格从 RON tu EUR 转换为我0在运营中获得的价格。

我使用的代码:

  include('./panou/simple_html_dom.php');
  function euro() {
       $dom= file_get_html("http://www.cursvalutar.ro/");;  
       foreach ($dom->find('tr') as $node) {
       if (is_a($node->children(1), 'simple_html_dom_node')) {
            if ($node->children(1)->plaintext == "Euro") {
            $plain = explode(',', $node->children(2)->plaintext);           
         if(!isset($plain[0])!== false) {
                      echo("Nu are");
                  }
          elseif(stripos($plain[0], $plain[0])!== false{
                      "4.30"
                  }
             }
        }
   }
   $plain[0]*=$koyos; echo "$plain[0]";}

$plain[0] = 4.3780脚本的$koyos = 545.66结果是0

4

2 回答 2

1

相反,使用此 URL 获取 RON-EUR 的实际价值:http: //download.finance.yahoo.com/d/quotes.csv ?s=RONEUR=X&f=sl1d1t1ba&e= .csv 解析 CSV 文件比解析 HTML 更容易文件。

于 2013-02-23T08:46:39.507 回答
0

代码问题:$koyos 变量未在 euro() 函数中定义。将其传递给函数。如果你想打印一个数组的元素,你应该这样做:

     echo $plain[0]
     echo "{$plain[0]}"    //  this two prints what you want 

这不是好方法:

     echo "$plain[0]"      // the result of this might be "Array[0]" 

但最好用这个替换你的代码:

    function getEuroRon(){
            if (!$file = fopen('http://download.finance.yahoo.com/d/quotes.csv?s=EURRON=X&f=sl1d1t1ba&e=.csv','r')){
                    die('resource can not be read');
            }
            $data = fgetcsv($file);
            fclose($file);
            return $data[1];
    }
    $euroron = getEuroRon();
    /*
    //... somewhere in code:
    $koyos = 545.66;

    //i dont recommend to use global variables but you might need this:
    function euro(){
          global $koyos,$euroron;
          print $euroron * $koyos;
    }
    */
    //This should be better instead the previous:
    function euro($koyos){
          global $euroron;
          print $euroron * $koyos;
    }   
    euro(545.66);

您可以在需要打印结果的地方调用 euro() 函数......应该有更好的方法,但我不知道您的代码的其他细节,并试图以几乎与您编码相同的方式解决您的问题。

或者您可以使用更好的解决方案:http: //pastebin.com/HMp8SR2j

于 2013-02-23T08:40:27.667 回答