【问题标题】:Dealing with "PHP HTML DOM Parser", including the same library into two different files处理“PHP HTML DOM Parser”,将同一个库包含到两个不同的文件中
【发布时间】:2013-06-25 16:29:32
【问题描述】:

我有两个 PHP 文件(相同的文件夹)可以访问库 simple_html_dom.php

第一个caridefine.php有这个:

include('simple_html_dom.php');
$url = 'http://www.statistics.com/index.php?page=glossary&term_id=209';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);
libxml_use_internal_errors(true);
$dom = new DomDocument();
$dom->loadHtml($curl_scraped_page);
$xpath = new DomXPath($dom);
print $xpath->evaluate('string(//p[preceding::b]/text())');

第二个,caridefine2.php 有这个:

include('simple_html_dom.php');
$url = 'http://www.statsoft.com//textbook/statistics-glossary/z/?button=0#Z Distribution (Standard Normal)';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_scraped_page = curl_exec($ch);

$html = new simple_html_dom();
$html->load($curl_scraped_page);
foreach ($html->find('/p/a [size="4"]') as $font) {
    $link = $font->parent();
    $paragraph = $link->parent();
    $text = str_replace($link->plaintext, '', $paragraph->plaintext);
    echo $text;
}

另外,它们每个都运行良好,我运行了caridefine.php,运行良好,caridefine2.php 也运行良好。 但是当我尝试在其他 PHP 文件中加载这两个文件时:

        <div class="examples">
            <?php
                $this->load->view('definer/caridefine.php');
            ?>
        </div>

        <div class="examples">
            <?php
               $this->load->view('definer/caridefine2.php');
            ?>
        </div>

他们都没有工作。刚给我一个空白页,当我按CTRL+U时,它说:Cannot redeclare file_get_html() (previously declared in C:\xampp\htdocs\MSPN\APPLICATION\views\Definer\simple_html_dom.php:70) in C:\xampp\htdocs\MSPN\APPLICATION\views\Definer\simple_html_dom.php on line 85

我在谷歌上搜索了这个问题,我发现“如果你加载许多对象而不清除以前的对象,这可能是个问题。” 我试过做$html-&gt;clear()unset($dom)。它什么也没给我。 是什么让我喜欢排在最后?

谢谢..

【问题讨论】:

    标签: php dom syntax html-parsing


    【解决方案1】:

    我试过分析我自己的问题: 这是更正:

    将每个文件中的include('simple_html_dom.php');改成:require_once('simple_html_dom.php');

    发生的事情是文件两次调用了文件simple_html_dom.php。所以它不会工作。 应该这样做。

    【讨论】:

    • 请接受您的回答,将问题标记为已解决。另外我认为您的问题与该库的内存泄漏没有直接关系,因此请更正标题,以免在搜索时产生误导。谢谢!
    • 是的,它对我说,我只能在两天后(也就是今天)接受我的回答,从现在开始还要等 11 个小时。如何更改标题?
    • 对,有这些时期。我刚刚注意到,有时用户看不到他们可以这样做,所以我留下了一个注释。 :) - 您可以通过编辑问题来更改问题标题。
    • 不,我的问题不是技术上如何做到这一点,而是“什么句子最能代替这个问题的标题”? ^^
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 2012-05-24
    • 1970-01-01
    • 2014-03-17
    相关资源
    最近更新 更多