首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能在一个类中加载简单html-dom类两次。

不能在一个类中加载简单html-dom类两次。
EN

Stack Overflow用户
提问于 2013-10-23 11:06:59
回答 1查看 792关注 0票数 3

当我试图将文件加载到dom对象中(在此之前清除它)时,dom变量是空的:(如何在类中使用simple-html-dom类两次?

代码语言:javascript
复制
<?php
require_once 'simple_html_dom.php';
class RO{
    public $dom;
    public $dom2;
    public function __construct(){
        $this->dom = new simple_html_dom();
        $this->dom2 = new simple_html_dom();
    }
    function GetDatumInUroPodatkovInTabKrajev($lang){
        $this->dom->load_file("http://www...diferentadresthenbelow...html");
        $tabelaTempInKrajev = $this->dom->find('table[@class="online"]');

        //some code...

        //some code...

        return $this->functon2("minkjaaa");
    }
    function functon2($ik){
        $this->dom2->load_file("http://www.arso.gov.si/vreme/napovedi%20in%20podatki/vreme_si.html");
        $tabelaVremRazm = $this->dom2->find('table[@class="online"]');    // this line trows an error, becaus dom2 is empty
        return"";
    }
}
?>

而trowen的错误是:

[Fatal error: Call to a member function find() on a non-object in C:\..\simple_html_dom.php on line ..]

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-23 13:34:07

除非指定了可选索引,否则Find("SELECTORS", index)查找将返回一个对象数组,然后返回Nth对象.这可能是你错过的..。下面是一个有用的例子:

代码语言:javascript
复制
// includes Simple HTML DOM Parser
include "simple_html_dom.php";

$url = "http://www.arso.gov.si/vreme/napovedi%20in%20podatki/vreme_si.html";

//Create a DOM object
$dom2 = new simple_html_dom();
// Load HTML from a string
$dom2->load_file($url);


// Find the link with the appropriate selectors
$table = $dom2->find('table.online', 0);


// Find succeeded
if ($table)
    echo $table->outertext;
else
    echo "Find function failed !";


// Clear DOM object (needed essentially when using many)
$dom2->clear(); 
unset($dom2);

在线演示

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19539998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档